R语言 为什么使用ggplot时,此条形图中误差线的位置不正确?

fwzugrvs  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(296)

看起来我的错误栏在我的情节中放置不正确,我不太明白为什么,我一定错过了什么...
我的图如下所示:

我的数据如下所示:

# A tibble: 9 × 6
# Groups:   variable [3]
  variable       gfp   stdev gfp_mean     ymax     ymin
  <chr>        <dbl>   <dbl>    <dbl>    <dbl>    <dbl>
1 gfp_sums   4286898 466912. 4478746  4945658. 4011834.
2 nc_sums     664845   4378.  662308.  666686.  657930.
3 media_sums  778269  29403.  744335   773738.  714932.
4 gfp_sums   5011021 466912. 4478746  4945658. 4011834.
5 nc_sums     657253   4378.  662308.  666686.  657930.
6 media_sums  726416  29403.  744335   773738.  714932.
7 gfp_sums   4138319 466912. 4478746  4945658. 4011834.
8 nc_sums     664827   4378.  662308.  666686.  657930.
9 media_sums  728320  29403.  744335   773738.  714932.

它是用这个

ggplot(gfp_long)+
  geom_bar( aes(x=variable, y=gfp_mean), stat="identity", fill="skyblue", alpha=0.7) +
  geom_errorbar( aes(x=variable, ymin=ymin, ymax=ymax), width=0.4, colour="orange", alpha=0.9, size=1.3)

先谢了。
输出 Dataframe :

structure(list(variable = c("gfp_sums", "nc_sums", "media_sums", 
"gfp_sums", "nc_sums", "media_sums", "gfp_sums", "nc_sums", "media_sums"
), gfp = c(4286898, 664845, 778269, 5011021, 657253, 726416, 
4138319, 664827, 728320), stdev = c(466911.593911524, 4378.05634195511, 
29403.1217900413, 466911.593911524, 4378.05634195511, 29403.1217900413, 
466911.593911524, 4378.05634195511, 29403.1217900413), gfp_mean = c(4478746, 
662308.333333333, 744335, 4478746, 662308.333333333, 744335, 
4478746, 662308.333333333, 744335), ymax = c(4945657.59391152, 
666686.389675288, 773738.121790041, 4945657.59391152, 666686.389675288, 
773738.121790041, 4945657.59391152, 666686.389675288, 773738.121790041
), ymin = c(4011834.40608848, 657930.276991378, 714931.878209959, 
4011834.40608848, 657930.276991378, 714931.878209959, 4011834.40608848, 
657930.276991378, 714931.878209959)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -9L), groups = structure(list(
    variable = c("gfp_sums", "media_sums", "nc_sums"), .rows = structure(list(
        c(1L, 4L, 7L), c(3L, 6L, 9L), c(2L, 5L, 8L)), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L), .drop = TRUE))
e5nqia27

e5nqia271#

同一个变量名重复了三次,所以它试图覆盖自身。
如果您查看table(gfp_long$variable),您会得到:

#  gfp_sums media_sums    nc_sums 
#         3          3          3 

gap_long[,1]

#  variable  
#  <chr>     
#1 gfp_sums  
#2 nc_sums   
#3 media_sums
#4 gfp_sums  
#5 nc_sums   
#6 media_sums
#7 gfp_sums  
#8 nc_sums   
#9 media_sums

因此,如果只绘制前三行(所有三个变量类型的第一个示例),它就能正常工作:

ggplot(gfp_long[1:3,]) +
  geom_bar(aes(x = variable, y = gfp_mean), stat = "identity", 
           fill = "skyblue", alpha = 0.7) +
  geom_errorbar(aes(x = variable, ymin = ymin, ymax = ymax), 
                width = 0.4, colour = "orange", alpha = 0.9, size = 1.3)

我不知道你想做什么来提供进一步的帮助。
您可能希望创建一个组变量和方面:

gfp_long$group <- rep(1:3, each = 3)

ggplot(gfp_long) +
  geom_bar(aes(x = variable, y = gfp_mean), stat = "identity", 
           fill = "skyblue", alpha = 0.7) +
  geom_errorbar(aes(x = variable, ymin = ymin, ymax = ymax), 
                width = 0.4, colour = "orange", alpha = 0.9, size = 1.3) +
  facet_grid(~group)

相关问题