gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:39:47 31 4
gpt4 key购买 nike

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

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

最佳答案

您可以通过将数据从宽格式转换为长格式来大大缩短代码。那么你只需要调用一次 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

关于r - 难以获得在 ggplot2 中指定颜色和形状的混合线 a 和点图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49015167/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com