结合两个变量 ggplot:geom_pointrange 和 geom_point
Combining two variables ggplot: geom_pointrange and geom_point
我试图在一张图中结合两个变量,
一个作为 geom_pointrange [因为我需要最小和最大表示(置信区间 2.5% 和 97.5% 及其中位数 (50%)]
其他变量是 geom_point,由其他微积分的中位数构成。
我发现 ggplot 做了这些表示,但我还没有把它们放在一起,作者:
#inputs
x <- seq(1:10)
n <- length(x)
yone <- 2 * runif(n)
ytwo <- runif(n)
ythree <- ytwo * 0.2
yfour <- ytwo * 2
df <- data.frame(x, yone, ytwo, ythree, yfour); df
library (ggplot2)
#yone and ytwo must be points
#ythree and yfour are min and max confidence interval (vertical line)
ggplot(df, aes(x, y = value, color = variable)) +
geom_pointrange(aes(ymin = ythree, ymax = yfour)) +
geom_point(aes(y = yone, col = "yone")) +
geom_point(aes(y = ytwo, col = "ytwo")) +
geom_line(aes(y = yfour))
有人能帮帮我吗?
这是一种可能的解决方案,可以获取您想要的情节类型。我已经使用 reshape2
包将您的数据更改为长格式。整形有很多选项,包括 tidyr
(聚集)、基础 R(整形)和 data.table
(融化)。对于融化的 data.frame 中的 yone
行,我已将置信区间设置为 NA
,因为您没有计算它们。最后,我将 geom_linerange
与 geom_point
一起使用,而不是 geom_pointrange
,以便优雅地处理 NA
值。
library(ggplot2)
library(reshape2)
# Reshape data to long form.
mdat <- melt(df, id.vars=c("x", "ythree", "yfour"))
# Set confidence intervals to NA for yone values,
# values for which you didn't compute CIs.
mdat[mdat$variable == "yone", c("ythree", "yfour")] <- NA
p = ggplot(data=mdat, aes(x=x, y=value, colour=variable, ymin=ythree, ymax=yfour)) +
geom_linerange() +
geom_point(size=3) +
geom_line(aes(y=yfour), linetype="dotted")
ggsave ("plot.png", p, height=4, width=6, dpi=150)
我试图在一张图中结合两个变量, 一个作为 geom_pointrange [因为我需要最小和最大表示(置信区间 2.5% 和 97.5% 及其中位数 (50%)]
其他变量是 geom_point,由其他微积分的中位数构成。
我发现 ggplot 做了这些表示,但我还没有把它们放在一起,作者:
#inputs
x <- seq(1:10)
n <- length(x)
yone <- 2 * runif(n)
ytwo <- runif(n)
ythree <- ytwo * 0.2
yfour <- ytwo * 2
df <- data.frame(x, yone, ytwo, ythree, yfour); df
library (ggplot2)
#yone and ytwo must be points
#ythree and yfour are min and max confidence interval (vertical line)
ggplot(df, aes(x, y = value, color = variable)) +
geom_pointrange(aes(ymin = ythree, ymax = yfour)) +
geom_point(aes(y = yone, col = "yone")) +
geom_point(aes(y = ytwo, col = "ytwo")) +
geom_line(aes(y = yfour))
有人能帮帮我吗?
这是一种可能的解决方案,可以获取您想要的情节类型。我已经使用 reshape2
包将您的数据更改为长格式。整形有很多选项,包括 tidyr
(聚集)、基础 R(整形)和 data.table
(融化)。对于融化的 data.frame 中的 yone
行,我已将置信区间设置为 NA
,因为您没有计算它们。最后,我将 geom_linerange
与 geom_point
一起使用,而不是 geom_pointrange
,以便优雅地处理 NA
值。
library(ggplot2)
library(reshape2)
# Reshape data to long form.
mdat <- melt(df, id.vars=c("x", "ythree", "yfour"))
# Set confidence intervals to NA for yone values,
# values for which you didn't compute CIs.
mdat[mdat$variable == "yone", c("ythree", "yfour")] <- NA
p = ggplot(data=mdat, aes(x=x, y=value, colour=variable, ymin=ythree, ymax=yfour)) +
geom_linerange() +
geom_point(size=3) +
geom_line(aes(y=yfour), linetype="dotted")
ggsave ("plot.png", p, height=4, width=6, dpi=150)