I am trying to crate a stacked bar chart but the first two bars are on top of each other instead of beside each other. The other bars are howewer correct.
I want to seperate them so that I'll have the 12 months as bars instad of the current 11 (on the x axis).
I checked if there are same values but that's not the case.
ggplot(rawdata4.1, aes(x= Monat, y= Ankünfte, fill= Jahreszahl))
facet_wrap(~ Herkunft, scale = "fixed")
geom_bar(position = "stack",
colour = "black",
lwd = 0.9,
linetype = 1,
stat = "identity",)
scale_y_continuous(name= NULL, labels = label_number(suffix = " Mio", scale = 1e-6))
scale_x_binned(name = NULL)
ggtitle("Monatliche Ankünfte vor und nach Corona")
theme_pander()
theme(axis.title.y = element_text(vjust = 1.75), legend.position = "right")
scale_fill_economist()
Below are two picture of the whole and final data set and my current code and a picture of my data.
CodePudding user response:
The issue is that you used scale_x_binned
. Drop that and convert your Monat
column to a factor
should fix your issue.
Using some fake example data let's first reproduce your issue:
set.seed(123)
rawdata4.1 <- data.frame(
Jahreszahl = rep(2019:2020, each = 24),
Monat = rep(1:12, each = 2),
Herkunft = c("Schweiz", "Alle Länder"),
Ankünfte = runif(48, 10000, 999999)
)
rawdata4.1$Jahreszahl <- factor(rawdata4.1$Jahreszahl)
library(ggplot2)
library(ggthemes)
ggplot(rawdata4.1, aes(x = Monat, y= Ankünfte, fill= Jahreszahl))
facet_wrap(~ Herkunft, scale = "fixed")
geom_bar(position = "stack",
colour = "black",
lwd = 0.9,
linetype = 1,
stat = "identity",)
scale_y_continuous(name= NULL, labels = scales::label_number(suffix = " Mio", scale = 1e-6))
scale_x_binned(name = NULL)
ggtitle("Monatliche Ankünfte vor und nach Corona")
theme_pander()
theme(axis.title.y = element_text(vjust = 1.75), legend.position = "right")
scale_fill_economist()
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
Now, dropping scale_x_binned
and converting to a factor
gives the desired result:
ggplot(rawdata4.1, aes(x = factor(Monat), y= Ankünfte, fill= Jahreszahl))
facet_wrap(~ Herkunft, scale = "fixed")
geom_bar(position = "stack",
colour = "black",
lwd = 0.9,
linetype = 1,
stat = "identity",)
scale_y_continuous(name= NULL, labels = scales::label_number(suffix = " Mio", scale = 1e-6))
ggtitle("Monatliche Ankünfte vor und nach Corona")
theme_pander()
theme(axis.title.y = element_text(vjust = 1.75), legend.position = "right")
scale_fill_economist()
CodePudding user response:
Like this?
df <- tibble(
year = rep(2019:2020, each = 24) %>% factor,
month = rep(rep(1:12,each = 2), 2) %>% factor,
group = rep(c("Schweiz", "Alle lander"), 24),
value = sample(10000:999999, replace = TRUE, 48)
)
df %>%
ggplot()
aes(x = month, y = value, fill = year)
geom_col(position = "dodge")
facet_wrap(~ group)
scale_fill_economist()
theme_pander()
CodePudding user response:
You can use scale_x_continuous
with breaks set at 1:12
library(ggplot2)
library(ggthemes)
library(scales)
ggplot(rawdata4.1, aes( x= Monat, y = Ankünfte, fill = Jahreszahl))
facet_wrap(~ Herkunft, scale = "fixed")
geom_col(position = "stack", colour = "black", lwd = 0.9, linetype = 1)
scale_y_continuous(labels = label_number(suffix = " Mio", scale = 1e-6),
name = NULL)
scale_x_continuous(NULL, breaks = 1:12)
scale_fill_economist()
ggtitle("Monatliche Ankünfte vor und nach Corona")
theme_pander()
theme(axis.title.y = element_text(vjust = 1.75),
legend.position = "right")
Data transcribed from question in reproducible format
rawdata4.1 <- data.frame(Jahreszahl = factor(rep(c(2019, 2020), each = 24)),
Monat = rep(rep(1:12, each = 2), 2),
Herkunft = rep(c("Schweiz", "Alle Länder"), 24),
Ankünfte = c(647315, 1226739, 698093, 1332892, 774956, 1526592,
623656, 1384180, 697684, 1624902, 858811, 2006805,
987515, 2284423, 1011069, 2270490, 926890,
1958831, 78940, 1612160, 589729, 1154630, 678702,
1381913, 707576, 1339067, 742885, 1356936, 296702,
498459, 43310, 53920, 289738, 318331, 606987,
725924, 1229947, 1574105, 1106300, 1545573,
1021787, 1304570, 811214, 957811, 353547, 424907,
484612, 603825))