geom和ggplot。美学的长度必须为1或与数据(11)相同:x,y,ymin,ymax

时间:2018-07-21 20:56:02

标签: r ggplot2

enter image description here我正在使用此脚本尝试创建这样的图形 我正在使用下面的数据和脚本,但是出现了以下消息:

错误:美学的长度必须为1或与数据(11)相同:x,y,ymin,ymax

The output of dput(graf1)is:
structure(list(est = structure(c(10L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 11L), .Label = c("A", "B", "C", "D", "E ", "F", "G", 
"H", "I ", "Intercept   ", "J"), class = "factor"), min = c(-2.59987657, 
1.90207329, 0.19540912, 0.38539491, 0.44573408, -0.62036798, 
-0.25411668, 0.12885761, -1.11518838, -0.08929551, -0.07907754
), max = c(-2.12964144, 2.31358683, 0.34741113, 0.53284866, 0.6833492, 
-0.32613644, 0.12285579, 0.46712478, -0.48094942, 0.05204147, 
0.06417294)), .Names = c("est", "min", "max"), class = "data.frame", row.names = c(NA, 
-11L))

script
graf1 <- read.csv(file="tab.csv", header = TRUE, sep = ";")
str(graf1)
attach(graf1)
names(graf1)
dput(graf1)

library(ggplot2)
ggplot(graf1, aes(x=graf1$X, y=graf1$est, ymin=graf1$min,ymax=graf1$max))+
  geom_pointrange()+
  geom_point()+
  coord_flip()+geom_hline(yintercept = 0, linetype="dotted")+ 
  xlab('Variable')
est min max
Intercept       -2.59987657 -2.12964144
A   1.90207329  2.31358683
B   0.19540912  0.34741113
C   0.38539491  0.53284866
D   0.44573408  0.6833492
E   -0.62036798 -0.32613644
F   -0.25411668 0.12285579
G   0.12885761  0.46712478
H   -1.11518838 -0.48094942
I   -0.08929551 0.05204147
J   -0.07907754 0.06417294

1 个答案:

答案 0 :(得分:1)

由于只有最小值和最大值,所以您可以做的最好的事情是geom_linerange

library(ggplot2)

df <- data.frame(est = c("Intercept", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"), 
                 min = c(-2.59987657, 1.90207329, 0.19540912, 0.38539491, 0.44573408, -0.62036798, -0.25411668, 0.12885761, -1.11518838, -0.08929551, -0.07907754),
                 max = c(-2.12964144, 2.31358683, 0.34741113, 0.53284866, 0.6833492, -0.32613644, 0.12285579, 0.46712478, -0.48094942, 0.05204147, 0.06417294))

ggplot(df, aes(factor(est, levels = rev(est)), ymin = min, ymax = max)) + 
    geom_linerange() + 
    geom_hline(yintercept = 0, linetype = "dotted") + 
    coord_flip() + 
    labs(x = 'Variable')

linerange coef plot