简体   繁体   English

创建图例到 ggplot2 线 plot

[英]Create legend to ggplot2 line plot

I have some questions about legends and axis in ggplot2.我对 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.我想在同一个图中 plot 两条线,并想用使用的线型和点添加一个图例(第 1 组和第 2 组)。

  2. Is there a possibility, to start the X-axis (R) at the origin?是否有可能从原点开始 X 轴 (R)? 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")) 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 #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() 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!非常感谢!

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.也许设置legend.position="bottom"将图例放在数字范围之外。

To your second question, if you want to expand the limits of your axes you can use expand_limits() .对于你的第二个问题,如果你想扩大你的轴的限制,你可以使用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) .您可以指定要扩展到的范围expand_limits(x = c(1,10)) ,也可以指定单个值,这听起来就像您在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.在我在上一句中提供的情况下,指定一个选项将保持自动设置的上限,但将下限扩展到原点。

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....我认为,一个问题是 geom_line 处的“颜色”修复了颜色....

      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...对于我的第二个问题:问题在于,x 轴由标称数据组成,没有限制……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM