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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了难以获得混合线a和指向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)
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
答案

这有点乱,但我认为它做你想要的。对不起怪异的颜色,我只是从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()

:)

另一答案

您可以通过将数据从宽格式转换为长格式来显着缩短代码。然后你只需要一次调用geom_pointgeom_line下面就是一个例子。 dat1dat2分别包含点和主线。 dat1已经是长格式。我们很好地将dat2转换为dat2longas。我把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

另一答案

正如评论中所建议的那样:让我们从清理数据框开始:

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

另一答案

你实际上非常接近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.

以上是关于难以获得混合线a和指向ggplot2中颜色和形状的点图例的主要内容,如果未能解决你的问题,请参考以下文章

画图笔记:ggplot2优化柱形图(添加误差线、差异比较分析)

使用 ggplot2 创建带有形状的图例

(R) 如何在 ggplot2 中根据颜色、线型和点形状获取图例?

《ggplot2:数据分析与图形艺术》笔记

在 ggplot2 中,使用两种图形类型时如何缩放图例?

ggplot2超详细讲解