难以获得混合线a和指向ggplot2中颜色和形状的点图例

时间:2018-02-27 18:04:08

标签: r ggplot2 legend

所以我一直在努力为这个情节找到一个正确的传奇。我的最终目标是显示线条颜色和点颜色的图例。我可以让传说中的线条工作正常,但是想知道我也是如何得到点数(并且包括形状点的颜色)?任何工作或建议非常感谢!

Tempx<-c(20, 22, 24, 26, 28, 30, 32, 34)
Tempx<-rep(Tempx,2)


Temp<-rnorm(1000,28,2)
var1<-rnorm(1000,25,5)
Data<-data.frame(Temp,var1)

Temp<-rnorm(1000,28,2)
var1<-rnorm(1000,12,5) 
Data2<-data.frame(Temp,var1)

a1<-1.05*Tempx
a2<-0.5*Tempx
a1low<-0.95*Tempx
a1high<-1.15*Tempx
a2low<-0.4*Tempx
a2high<-0.6*Tempx


plot1<-ggplot(NULL, aes(Temp, var1)) + 
  geom_point(data = Data, colour="grey60", size=1.5, shape=1, show.legend=TRUE) +
  geom_point(data = Data2, shape= 16, size=1, show.legend=TRUE) +
  geom_line(aes(x=Tempx,y=a1, colour="grey50"), size=1.75, show.legend=TRUE) +
  geom_line(aes(x=Tempx,y=a2, colour="black"), size=1.75, show.legend=TRUE) +
  geom_line(aes(x=Tempx,y=a1low), colour="grey50",size=1.25, linetype="longdash") +
  geom_line(aes(x=Tempx,y=a1high), colour="grey50",size=1.25, linetype="longdash") +
  geom_line(aes(x=Tempx,y=a2low), colour="black",size=1.25, linetype="longdash") +
  geom_line(aes(x=Tempx,y=a2high), colour="black",size=1.25, linetype="longdash") +
  scale_shape_manual(values=c(1, 16)) +
  scale_color_manual(labels = c("low", "high"), values=c('black','grey50')) +
  theme_bw()+
  theme(axis.line.x = element_line(colour = "black"),
        axis.line.y = element_line(colour = "black"),
        axis.text.x = element_text(margin=unit(c(0.4,0.4,0.4,0.4), "cm")), 
        axis.text.y = element_text(margin=unit(c(0.4,0.4,0.4,0.4), "cm")),
        axis.title.x = element_text(margin = margin(t = -6)),
        axis.title.y = element_text(margin = margin(t = -6)),
        axis.text=element_text(size=13),
        text = element_text(size=14),
        plot.title = element_text(size=16, hjust=0),
        axis.ticks.length = unit(-0.1,"cm"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank()) +
  scale_x_continuous(name = "Temperature", breaks = c(20, 22, 24, 26, 28, 30, 32, 34), expand=c(0,0), limits=c(20,34))+
  scale_y_continuous(name = "Variable 1", expand = c(0, 0), limits = c(0, 40)) +
  ggtitle("Title")

plot1

4 个答案:

答案 0 :(得分:5)

您可以通过将数据从宽格式转换为长格式来显着缩短代码。然后,您只需拨打一次geom_pointgeom_line即可。以下是一个示例。 dat1dat2分别包含点和主线。 dat1已经是长格式的。我们也将dat2转换为dat2long。我已将dat3long放在一个单独的数据框中,因为我们不需要将这些值映射到产生图例的美学。因此,我对geom_line单独使用了这些行,并且我已对这些行进行了硬编码。

library(tidyverse)

# Create long data frame for point data
dat1 = bind_rows(list(a1=Data, a2=Data2), .id="Source")

# Create long data frames for lines data
dat2 = data.frame(Tempx, a1, a2)
dat3 = data.frame(Tempx, a1low, a1high, a2low, a2high)
dat2long = dat2 %>% gather(Source, value, -Tempx)
dat3long = dat3 %>% gather(Source, value, -Tempx)

ggplot() +
  geom_point(data=dat1, aes(Temp, var1, shape=Source, colour=Source)) +
  geom_line(data=dat2long, aes(x=Tempx, y=value, colour=Source)) +
  geom_line(data=dat3long, aes(x=Tempx, y=value, group=Source), linetype=2, 
            colour=rep(c("red","blue"), each=2*length(Tempx))) +
  scale_colour_manual(values=c("red","blue")) +
  theme_bw()

enter image description here

如果你的目标是绘制置信区间,你可以这样做:

ggplot(data=dat1, aes(Temp, var1, shape=Source, fill=Source, colour=Source)) +
  geom_point() +
  geom_smooth(method='lm') +
  theme_bw()

enter image description here

答案 1 :(得分:1)

根据评论中的建议: 让我们从清理数据框开始:

Temp<-rnorm(1000,28,2)
Data1<-data.frame(Temp,var = rnorm(1000,25,5), group = rep ('a', 1000)) ##adding groups
Data2<-data.frame(Temp,var = rnorm(1000,12,5), group = rep ('b', 1000))

data = rbind(Data1, Data2 )

现在正在清理您的ggplot电话

建议:

ggplot(data, aes(Temp, var, color = group )) + 
geom_point(size=1.5, shape=1) + 
geom_smooth(method = 'lm') #(maybe this model is what you want anyways)

enter image description here

答案 2 :(得分:1)

这有点乱,但我认为它符合您的要求。对不起那些怪异的颜色,我只是从colorbrewer中抽出一些颜色。

library(ggplot2)

Tempx<-c(20, 22, 24, 26, 28, 30, 32, 34)
Tempx<-rep(Tempx,2)


Temp<-rnorm(1000,28,2)
var1<-rnorm(1000,25,5)
var2<-rnorm(1000,12,5) 

a1<-1.05*Tempx
a2<-0.5*Tempx
a1low<-0.95*Tempx
a1high<-1.15*Tempx
a2low<-0.4*Tempx
a2high<-0.6*Tempx


dat1 <- data.frame(Temp,  var1, var2)
dat2 <- data.frame(Tempx, a1, a2, a1low, a1high, a2low, a2high)


plot2 <- ggplot() +
geom_point(data=dat1,aes(x=Temp,y=var1, colour="var1", size=1.5)) +
geom_point(data=dat1,aes(x=Temp, y=var2, colour="var2", size=1)) +
geom_line(data=dat2,aes(x=Tempx, y=a1, colour="a1"), size=1.75) +
geom_line(data=dat2,aes(x=Tempx,y=a2, colour="a2"), size=1.75) +
geom_line(data=dat2,aes(x=Tempx,y=a1low, colour="black",size=1.25, linetype="longdash")) +
geom_line(data=dat2,aes(x=Tempx,y=a1high, colour="black",size=1.25, linetype="longdash")) +
geom_line(data=dat2,aes(x=Tempx,y=a2low, colour="black",size=1.25, linetype="longdash")) +
geom_line(data=dat2,aes(x=Tempx,y=a2high, colour="black",size=1.25, linetype="longdash")) +
scale_color_manual("",
    breaks = c("var1", "var2", "a1", "a2", "a1low", "a1high", "a2low", "a2high"), 
    values=c("#762a83", "#af8dc3","#e7d4e8","#f7f7f7","#d9f0d3","#7fbf7b","#1b7837", "#000000")) +
scale_size(guide = "none")+
guides(size="none", linetype="none")+ #removes linetype and sizes from legend
theme_bw()

:)

答案 3 :(得分:1)

您实际上非常接近scale_shape_manualscale_colour_manual来电。缺少的成分是将两组点的形状放入geom_point的映射中,就像geom_line中的颜色一样:

library(ggplot2)

plot2 <- ggplot(mapping=aes(x=Temp, y=var1)) +

  geom_point(data=Data, mapping=aes(shape="high"), colour="grey50", size=1.5) +
  geom_point(data=Data2, mapping=aes(shape="low")) +
  scale_shape_manual(
    values=c(1, 16),
    guide=guide_legend(
      title="Legend: Points",
      override.aes=list(
        colour=c("grey50", "black"),
        size=c(1.5, 1)
      )
    )
  ) +

  geom_line(mapping=aes(x=Tempx, y=a1, colour="high"), size=1.75) +
  geom_line(mapping=aes(x=Tempx, y=a2, colour="low"), size=1.75) +
  scale_colour_manual(values=c("grey50", "black"), guide=guide_legend(title="Legend: Lines")) +

  geom_line(mapping=aes(x=Tempx, y=a1low), colour="grey50", size=1.25, linetype="longdash") +
  geom_line(mapping=aes(x=Tempx, y=a1high), colour="grey50", size=1.25, linetype="longdash") +
  geom_line(mapping=aes(x=Tempx, y=a2low), colour="black", size=1.25, linetype="longdash") +
  geom_line(mapping=aes(x=Tempx, y=a2high), colour="black", size=1.25, linetype="longdash") +

  theme_bw() +
  theme(axis.line.x=element_line(colour="black"),
        axis.line.y=element_line(colour="black"),
        axis.text.x=element_text(margin=unit(c(0.4, 0.4, 0.4, 0.4), "cm")), 
        axis.text.y=element_text(margin=unit(c(0.4, 0.4, 0.4, 0.4), "cm")),
        axis.title.x=element_text(margin=margin(t=-6)),
        axis.title.y=element_text(margin=margin(t=-6)),
        axis.text=element_text(size=13),
        text=element_text(size=14),
        plot.title=element_text(size=16, hjust=0),
        axis.ticks.length=unit(-0.1,"cm"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        panel.border=element_blank(),
        panel.background=element_blank()) +
  scale_x_continuous(name="Temperature", breaks=c(20, 22, 24, 26, 28, 30, 32, 34), expand=c(0, 0), limits=c(20, 34)) +
  scale_y_continuous(name="Variable 1", expand=c(0, 0), limits=c(0, 40)) +
  ggtitle("Title")

上面的代码给出了一个带有两个单独图例的图,一个用于点,一个用于线:

A plot with two separate legends, one for points and one for lines.