Reputation: 77
i have a dataframe
df <-data.frame(apport=c("Min 1", "Min 2", "Org 2017","Org 2017","Org 2017",
"Org 2017","Org 2017","Org 2017","Org 2017","Org 2017",
"Org 2017","Org 2017"), periode_ap=c("Mai_32","Jui_34",
"Avr_30", "Avr_31","Avr_31","Avr_31","Mai_32","Mai_32","Mai_32",
"Mai_33","Mai_33","Mai_33"))
I made a ggplot
ggplot(df, aes(x=periode_ap, color=apport))+geom_density()
but i want to have in the x axis a graduation that i have in this list :
list<-c("Jan_0", "Jan_1", "Fev_2", "Fev_3","Mar_4", "Mar_5", "Avr_6", "Avr_7", "Mai_8", "Mai_9", "Jui_10", "Jui_11", "Jul_12", "Jul_13", "Aou_14", "Aou_15", "Sep_16", "Sep_17", "Oct_18", "Oct_19", "Nov_20","Nov_21", "Dec_22", "Dec_23", "Jan_24", "Jan_25", "Fev_26", "Fev_27", "Mar_28", "Mar_29", "Avr_30","Avr_31", "Mai_32", "Mai_33", "Jui_34", "Jui_35", "Jul_36", "Jul_37", "Aou_38",
"Aou_39", "Sep_40", "Sep_41", "Oct_42", "Oct_43", "Nov_44", "Nov_45", "Dec_46", "Dec_47")
to have a periode from Jan_0 until Dec_47 instead of Avr_30 until Jui_34 that I have in my data, if you can help me , thanks
Upvotes: 0
Views: 546
Reputation: 13853
Seems like you're dancing around the answer a bit. The problem lies in that you cannot use something like xlim('Jan_0','Dec_47')
because your x axis scale is discrete and not continuous. ggplot2
has no idea how your data should be organized, so it is just defaulting to as it exists in the dataframe and does not know how to extrapolate back to "Jan_0" or further out to "Dec_47". (Incidentally... neither do I...).
The answer to how you expand your chart is therefore twofold:
(1) Tell ggplot2
how your data is organized. I'm setting df$periode_ap
as a factor, and indicating the levels are your list.
lev_periode_ap <- list<-c("Jan_0", "Jan_1", "Fev_2", "Fev_3","Mar_4", "Mar_5", "Avr_6", "Avr_7", "Mai_8", "Mai_9", "Jui_10", "Jui_11", "Jul_12", "Jul_13", "Aou_14", "Aou_15", "Sep_16", "Sep_17", "Oct_18", "Oct_19", "Nov_20","Nov_21", "Dec_22", "Dec_23", "Jan_24", "Jan_25", "Fev_26", "Fev_27", "Mar_28", "Mar_29", "Avr_30","Avr_31", "Mai_32", "Mai_33", "Jui_34", "Jui_35", "Jul_36", "Jul_37", "Aou_38",
"Aou_39", "Sep_40", "Sep_41", "Oct_42", "Oct_43", "Nov_44", "Nov_45", "Dec_46", "Dec_47")
df$period_ap <- factor(df$periode_ap, levels=lev_periode_ap)
(2) Tell ggplot2
what to do with those levels. Now that ggplot2
knows that df$periode_ap
is ordinal, it can tell what is "highest", what is "lowest", and critically the order of all of those levels. We need to indicate that we want all levels to be shown via scale_x_discrete(labels=...)
, but importantly, we want to be sure ggplot2
does not drop any levels where there is no data. Lucky for us we can use the drop=
argument for that. After a bit of theme()
element changes, you get this:
ggplot(df, aes(x=period_ap,color=apport))+ theme_bw() +
geom_density() +
scale_x_discrete(labels=lev_periode_ap, drop=FALSE) +
theme(axis.text.x=element_text(angle=90, vjust=0.5, hjust=1))
Now, seems we're missing enough datapoints to make showing the colors for "Min 1" and "Min 2" meaningful, but there's the answer to the question you posed.
Upvotes: 2