如何使用ggplot绘制两列数据框列而不融化
how to plot two columns of data frame columns with ggplot and without melting
我创建了一个数据框,其中包含一组系数及其标准差,如下所示:
x co_crowd sd_co_crowd co_not_crowd sd_co_not_crowd
1 0.60 0.43012132 0.1747872 0.3966671 0.1687380
2 0.62 0.46063333 0.1773200 0.3377529 0.1660473
3 0.64 0.46269688 0.1828233 0.3298026 0.1611410
4 0.66 0.40083469 0.1859609 0.3821874 0.1579615
5 0.68 0.35852075 0.1875722 0.3772546 0.1565694
6 0.70 0.28980734 0.1910099 0.4147173 0.1547535
7 0.72 0.33115725 0.1995733 0.3605751 0.1499210
8 0.74 0.37740694 0.2050877 0.3499887 0.1471462
9 0.76 0.33312104 0.2065729 0.3349059 0.1463895
10 0.78 0.33023339 0.2087189 0.3309792 0.1444242
11 0.80 0.20648732 0.2226057 0.4126518 0.1408211
12 0.82 0.08671806 0.2333934 0.5076005 0.1374987
13 0.84 0.09258312 0.2418525 0.4890796 0.1356016
14 0.86 0.08282405 0.2712092 0.4910314 0.1318858
15 0.88 0.05364842 0.2880257 0.5179220 0.1296971
16 0.90 -0.09106470 0.3429453 0.5345661 0.1263504
我想做的是绘制 co_crowd 列及其误差线 (sd_co_crowd ) 以及 co_not_crowd 及其相关误差线 (sd_co_not_crowd)它。最初,我写了下面的代码来绘图:
plot(x, co_crowd, type="o", col="blue", ylim=c(-0.1,0.7), xlab="",ylab="")
lines(x, co_not_crowd, type="o", pch=22, lty=2, col="red", xlab="",ylab="")
title(main="Coeff results", col.main="red", font.main=4)
legend(0.6, 0.1, legend=c("non_crowd", "crowd"),col=c("red", "blue"), lty=1:2, cex=1.2)
grid(nx = NULL, ny = NULL, col = "lightgray", lty = "dotted")
title(xlab= "Thresholds")
title(ylab= "Coefficients")
结果是这样的:
现在我想给这个图添加一个错误栏;但是,我找到的函数是 geom_errorbar
,它是用于 ggplot 的,不能在这里使用。所以,为了使用它,我必须使用 ggplot
。现在我想知道如何在不以线和点的形式融合的情况下绘制两列数据框? (就像我最初绘制的那样)
所以最后,我想要的是这种形式的东西:
通过使用 ggplot。 (为了演示目的,我用 excel 绘制了它)
我建议采用这种方法。只是一些想法,因为需要非熔化方法,您需要在单独的 geom 中添加变量。优点是变量很少。此外,为了显示图例,您可以在 aes()
中添加美学元素 color
,以便创建图例,最后您可以使用 scale_color_manual()
更改颜色。关于误差条,我构建它的方式是使用 value-sd
但您可以考虑使用均值和标准差等其他方法。这里的代码:
library(ggplot2)
#Code
ggplot(df,aes(x=x,y=co_crowd))+
geom_line(aes(color='crowd'))+
geom_point(aes(color='crowd'))+
geom_errorbar(aes(ymin=co_crowd-sd_co_crowd,
ymax=co_crowd+sd_co_crowd,color='crowd'))+
geom_line(aes(x=x,y=co_not_crowd,color='not_crowd'))+
geom_point(aes(x=x,y=co_not_crowd,color='not_crowd'))+
geom_errorbar(aes(ymin=co_not_crowd-sd_co_not_crowd,
ymax=co_not_crowd+sd_co_not_crowd,color='not_crowd'))+
theme_bw()+
theme(legend.position = 'top',
axis.text = element_text(color='black',face='bold'),
axis.title = element_text(color='black',face='bold'),
legend.text = element_text(color='black',face='bold'),
legend.title = element_text(color='black',face='bold'))+
labs(color='Variable',y='')+
scale_color_manual(values=c('red','blue'))
输出:
使用了一些数据:
#Data
df <- structure(list(x = c(0.6, 0.62, 0.64, 0.66, 0.68, 0.7, 0.72,
0.74, 0.76, 0.78, 0.8, 0.82, 0.84, 0.86, 0.88, 0.9), co_crowd = c(0.43012132,
0.46063333, 0.46269688, 0.40083469, 0.35852075, 0.28980734, 0.33115725,
0.37740694, 0.33312104, 0.33023339, 0.20648732, 0.08671806, 0.09258312,
0.08282405, 0.05364842, -0.0910647), sd_co_crowd = c(0.1747872,
0.17732, 0.1828233, 0.1859609, 0.1875722, 0.1910099, 0.1995733,
0.2050877, 0.2065729, 0.2087189, 0.2226057, 0.2333934, 0.2418525,
0.2712092, 0.2880257, 0.3429453), co_not_crowd = c(0.3966671,
0.3377529, 0.3298026, 0.3821874, 0.3772546, 0.4147173, 0.3605751,
0.3499887, 0.3349059, 0.3309792, 0.4126518, 0.5076005, 0.4890796,
0.4910314, 0.517922, 0.5345661), sd_co_not_crowd = c(0.168738,
0.1660473, 0.161141, 0.1579615, 0.1565694, 0.1547535, 0.149921,
0.1471462, 0.1463895, 0.1444242, 0.1408211, 0.1374987, 0.1356016,
0.1318858, 0.1296971, 0.1263504)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16"))
之后,您可以根据需要调整其他元素颜色的任何其他细节。
我创建了一个数据框,其中包含一组系数及其标准差,如下所示:
x co_crowd sd_co_crowd co_not_crowd sd_co_not_crowd
1 0.60 0.43012132 0.1747872 0.3966671 0.1687380
2 0.62 0.46063333 0.1773200 0.3377529 0.1660473
3 0.64 0.46269688 0.1828233 0.3298026 0.1611410
4 0.66 0.40083469 0.1859609 0.3821874 0.1579615
5 0.68 0.35852075 0.1875722 0.3772546 0.1565694
6 0.70 0.28980734 0.1910099 0.4147173 0.1547535
7 0.72 0.33115725 0.1995733 0.3605751 0.1499210
8 0.74 0.37740694 0.2050877 0.3499887 0.1471462
9 0.76 0.33312104 0.2065729 0.3349059 0.1463895
10 0.78 0.33023339 0.2087189 0.3309792 0.1444242
11 0.80 0.20648732 0.2226057 0.4126518 0.1408211
12 0.82 0.08671806 0.2333934 0.5076005 0.1374987
13 0.84 0.09258312 0.2418525 0.4890796 0.1356016
14 0.86 0.08282405 0.2712092 0.4910314 0.1318858
15 0.88 0.05364842 0.2880257 0.5179220 0.1296971
16 0.90 -0.09106470 0.3429453 0.5345661 0.1263504
我想做的是绘制 co_crowd 列及其误差线 (sd_co_crowd ) 以及 co_not_crowd 及其相关误差线 (sd_co_not_crowd)它。最初,我写了下面的代码来绘图:
plot(x, co_crowd, type="o", col="blue", ylim=c(-0.1,0.7), xlab="",ylab="")
lines(x, co_not_crowd, type="o", pch=22, lty=2, col="red", xlab="",ylab="")
title(main="Coeff results", col.main="red", font.main=4)
legend(0.6, 0.1, legend=c("non_crowd", "crowd"),col=c("red", "blue"), lty=1:2, cex=1.2)
grid(nx = NULL, ny = NULL, col = "lightgray", lty = "dotted")
title(xlab= "Thresholds")
title(ylab= "Coefficients")
结果是这样的:
现在我想给这个图添加一个错误栏;但是,我找到的函数是 geom_errorbar
,它是用于 ggplot 的,不能在这里使用。所以,为了使用它,我必须使用 ggplot
。现在我想知道如何在不以线和点的形式融合的情况下绘制两列数据框? (就像我最初绘制的那样)
所以最后,我想要的是这种形式的东西:
通过使用 ggplot。 (为了演示目的,我用 excel 绘制了它)
我建议采用这种方法。只是一些想法,因为需要非熔化方法,您需要在单独的 geom 中添加变量。优点是变量很少。此外,为了显示图例,您可以在 aes()
中添加美学元素 color
,以便创建图例,最后您可以使用 scale_color_manual()
更改颜色。关于误差条,我构建它的方式是使用 value-sd
但您可以考虑使用均值和标准差等其他方法。这里的代码:
library(ggplot2)
#Code
ggplot(df,aes(x=x,y=co_crowd))+
geom_line(aes(color='crowd'))+
geom_point(aes(color='crowd'))+
geom_errorbar(aes(ymin=co_crowd-sd_co_crowd,
ymax=co_crowd+sd_co_crowd,color='crowd'))+
geom_line(aes(x=x,y=co_not_crowd,color='not_crowd'))+
geom_point(aes(x=x,y=co_not_crowd,color='not_crowd'))+
geom_errorbar(aes(ymin=co_not_crowd-sd_co_not_crowd,
ymax=co_not_crowd+sd_co_not_crowd,color='not_crowd'))+
theme_bw()+
theme(legend.position = 'top',
axis.text = element_text(color='black',face='bold'),
axis.title = element_text(color='black',face='bold'),
legend.text = element_text(color='black',face='bold'),
legend.title = element_text(color='black',face='bold'))+
labs(color='Variable',y='')+
scale_color_manual(values=c('red','blue'))
输出:
使用了一些数据:
#Data
df <- structure(list(x = c(0.6, 0.62, 0.64, 0.66, 0.68, 0.7, 0.72,
0.74, 0.76, 0.78, 0.8, 0.82, 0.84, 0.86, 0.88, 0.9), co_crowd = c(0.43012132,
0.46063333, 0.46269688, 0.40083469, 0.35852075, 0.28980734, 0.33115725,
0.37740694, 0.33312104, 0.33023339, 0.20648732, 0.08671806, 0.09258312,
0.08282405, 0.05364842, -0.0910647), sd_co_crowd = c(0.1747872,
0.17732, 0.1828233, 0.1859609, 0.1875722, 0.1910099, 0.1995733,
0.2050877, 0.2065729, 0.2087189, 0.2226057, 0.2333934, 0.2418525,
0.2712092, 0.2880257, 0.3429453), co_not_crowd = c(0.3966671,
0.3377529, 0.3298026, 0.3821874, 0.3772546, 0.4147173, 0.3605751,
0.3499887, 0.3349059, 0.3309792, 0.4126518, 0.5076005, 0.4890796,
0.4910314, 0.517922, 0.5345661), sd_co_not_crowd = c(0.168738,
0.1660473, 0.161141, 0.1579615, 0.1565694, 0.1547535, 0.149921,
0.1471462, 0.1463895, 0.1444242, 0.1408211, 0.1374987, 0.1356016,
0.1318858, 0.1296971, 0.1263504)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16"))
之后,您可以根据需要调整其他元素颜色的任何其他细节。