0
votes

I have some questions about legends and axis in ggplot2.

  1. I want to plot two lines in the same graph and want to add a legend (group 1 and group 2) with the used linetypes and points.

  2. Is there a possibility, to start the X-axis (R) at the origin? I know that a bar chart would actually be the correct choice, but I want to use a line chart for content reasons. There is my code:

    set.seed(1234) data <- data.frame( X = sample(1:6), Y = sample(1:6)) dim=c("R","I","A","S","E","C") datadim<-cbind(dim,data) datadim$dim <- factor(datadim$dim,levels = c("R","I","A","S","E","C"))

    #Plot erzeugen

    ggplot(data=datadim, aes(x=dim, group=2))+ geom_line(aes(y=X),linetype=1, size=1)+ geom_point(aes(y=X), size=2, shape=1)+ geom_line(aes(y=Y),linetype=2, size=1)+ geom_point(aes(y=Y), size=2, shape=4)+ labs(x="", y="Int")+ scale_y_continuous(limits=c(0, 6), breaks = seq(0, 6, by=0.5))+ geom_rangeframe()+ theme_classic()

Thank you very much!

2
Does your legend just not show up at all? Providing some test data and expected output would help us to provide the answers you're looking for. Check out this post: stackoverflow.com/questions/5963269/…Jeffrey Brabec

2 Answers

0
votes

To your first question, I was under the impression that specifying "linetype" and "size" would automatically generate the legend. Maybe setting legend.position="bottom" is placing the legend outside of the figures bounds.

To your second question, if you want to expand the limits of your axes you can use expand_limits(). You can either specify a range to expand to, expand_limits(x = c(1,10)) or you can specify a single value, which sounds like what you're after expand_limits(x = 0). Specifying a single option, in the case I provided in the previous sentence, will keep the auto-made upper bound, but extend the lower bound to the origin.

0
votes

Thank you for help. Now, I found a solution to generate the legend (first question). One problem was, that I thought, that "color" at geom_line fixes the color....

      ggplot(data=mwplots_p_t, aes(x=dim, group=1))+
  geom_line(aes(y=X1, color="1",linetype="1"), size=1)+
  geom_point(aes(y=X1, color="1"), size=2)+
  geom_line(aes(y=X2, color="2",linetype="2"), size=1)+
  geom_point(aes(y=X2, color="2"), size=2)+
  geom_line(aes(y=X3, color="3",linetype="3"), size=1)+
  geom_point(aes(y=X3, color="3"), size=2)+
  labs(x="", y="")+
  theme_classic()+
  scale_y_continuous(limits=c(-2, 2), breaks=seq(-2, 2, by=0.5))+
  theme(legend.position="bottom")+
  scale_color_manual(values=c("black", "black", "black"), name=NULL, labels=c("1", "2", "3"))+
  scale_linetype_manual(values=c("1"=1, "2"=2,"3"=3), name=NULL, labels=c("1", "2", "3"))+
  guides(fill = guide_legend(keywidth = 1, keyheight = 1),
           linetype=guide_legend(keywidth = 3, keyheight = 1),
           colour=guide_legend(keywidth = 3, keyheight = 1))

To my second question: there is the problem, that the x-axis consists of nominal data, which has no limits...