Ggplot条形图不显示任何列[关闭]

Posted

技术标签:

【中文标题】Ggplot条形图不显示任何列[关闭]【英文标题】:Ggplot bar plot not displaying any columns [closed] 【发布时间】:2022-01-10 04:22:04 【问题描述】:

我是 R 的初学者,并且有以下相关表(这是完整的):

Master_Correlation:

    Correlation to Hours Worked        Explanatory Variable     Race
 1:               -0.0045128223                     forborn    white
 2:                0.0323171103                    numchild    white
 3:               -0.1504050423 female numchild interaction    white
 4:               -0.2511609551                      female    white
 5:               -0.0383993120                          hs    white
 6:               -0.0571333231                     somecol    white
 7:                0.0266860952                     college    white
 8:                0.0859899460                    advanced    white
 9:                0.1392112264                          rw    white
10:                0.0273427376                  rw squared    white
11:               -0.0207199322                     forborn    black
12:               -0.0008369800                    numchild    black
13:               -0.0979065820 female numchild interaction    black
14:               -0.1678122716                      female    black
15:               -0.0584001709                          hs    black
16:               -0.0342631733                     somecol    black
17:                0.0585449347                     college    black
18:                0.0987732746                    advanced    black
19:                0.1461246589                          rw    black
20:                0.0348695563                  rw squared    black
21:               -0.0208368904                     forborn hispanic
22:                0.0174493239                    numchild hispanic
23:               -0.1635641693 female numchild interaction hispanic
24:               -0.2438919429                      female hispanic
25:               -0.0076820903                          hs hispanic
26:               -0.0337691135                     somecol hispanic
27:                0.0376874627                     college hispanic
28:                0.0841707245                    advanced hispanic
29:                0.1515369812                          rw hispanic
30:                0.0637695725                  rw squared hispanic
31:               -0.0274184696                     forborn    asian
32:                0.0159808674                    numchild    asian
33:               -0.1081184916 female numchild interaction    asian
34:               -0.1676908858                      female    asian
35:               -0.0576837324                          hs    asian
36:               -0.0608660949                     somecol    asian
37:               -0.0007628678                     college    asian
38:                0.1181154823                    advanced    asian
39:                0.1467355792                          rw    asian
40:                0.0786165669                  rw squared    asian
41:               -0.0106969158                     forborn    other
42:                0.0241509708                    numchild    other
43:               -0.1370435450 female numchild interaction    other
44:               -0.1869553011                      female    other
45:               -0.0391851362                          hs    other
46:               -0.0134763887                     somecol    other
47:                0.0544616829                     college    other
48:                0.0602647172                    advanced    other
49:                0.2082363589                          rw    other
50:                0.1210709059                  rw squared    other

请注意,我有 5 场比赛,每场比赛都有一个与 10 个解释变量相关的相关观察。我的目标是制作一个条形图,其中 x 轴是 10 个解释变量的名称,其中有 5 个条形对应于每场比赛的观察。我尝试了以下方法,但打印的是空白图。非常感谢任何帮助(尤其是反馈告诉我为什么这不起作用,以便我可以更好地编码)!

ggplot(Master_Correlation, aes(x= `Explanatory Variable`, y= `Correlation to Hours Worked`, 
                               fill= Race)) 

数据:

structure(list(`Correlation to Hours Worked` = c(-0.0045128223, 
0.0323171103, -0.1504050423, -0.2511609551, -0.038399312, -0.0571333231, 
0.0266860952, 0.085989946, 0.1392112264, 0.0273427376, -0.0207199322, 
-0.00083698, -0.097906582, -0.1678122716, -0.0584001709, -0.0342631733, 
0.0585449347, 0.0987732746, 0.1461246589, 0.0348695563, -0.0208368904, 
0.0174493239, -0.1635641693, -0.2438919429, -0.0076820903, -0.0337691135, 
0.0376874627, 0.0841707245, 0.1515369812, 0.0637695725, -0.0274184696, 
0.0159808674, -0.1081184916, -0.1676908858, -0.0576837324, -0.0608660949, 
-0.0007628678, 0.1181154823, 0.1467355792, 0.0786165669, -0.0106969158, 
0.0241509708, -0.137043545, -0.1869553011, -0.0391851362, -0.0134763887, 
0.0544616829, 0.0602647172, 0.2082363589, 0.1210709059), `Explanatory Variable` = c("forborn", 
"numchild", "female numchild interaction", "female", "hs", "somecol", 
"college", "advanced", "rw", "rw squared", "forborn", "numchild", 
"female numchild interaction", "female", "hs", "somecol", "college", 
"advanced", "rw", "rw squared", "forborn", "numchild", "female numchild interaction", 
"female", "hs", "somecol", "college", "advanced", "rw", "rw squared", 
"forborn", "numchild", "female numchild interaction", "female", 
"hs", "somecol", "college", "advanced", "rw", "rw squared", "forborn", 
"numchild", "female numchild interaction", "female", "hs", "somecol", 
"college", "advanced", "rw", "rw squared"), Race = c("white", 
"white", "white", "white", "white", "white", "white", "white", 
"white", "white", "black", "black", "black", "black", "black", 
"black", "black", "black", "black", "black", "hispanic", "hispanic", 
"hispanic", "hispanic", "hispanic", "hispanic", "hispanic", "hispanic", 
"hispanic", "hispanic", "asian", "asian", "asian", "asian", "asian", 
"asian", "asian", "asian", "asian", "asian", "other", "other", 
"other", "other", "other", "other", "other", "other", "other", 
"other")), row.names = c(NA, -50L), class = c("tbl_df", "tbl", 
"data.frame"))

【问题讨论】:

【参考方案1】:

OP,你快到了。您已经使用所有必要的映射设置了绘图区域和环境(即,xy 是什么,以及与fill 颜色对应的是什么)。问题是你没有告诉ggplot你想画点什么!。在ggplot 术语中,这意味着指定一个geom。

在这种情况下,您有 y 和 x 值,因此您需要“柱状”图(严格来说),而不是“条形”图(需要 x 值列表)。只需将+ geom_col() 添加到您的代码中。试试这个:

ggplot(Master_Correlation, aes(x=`Explanatory Variable`, y=`Correlation to Hours Worked`, fill= Race)) +
geom_col()

这将绘制列,但是显示具有相同 x 值的不同颜色的列的默认方法是将它们“堆叠”在彼此的顶部。为了确保它们根据数据中的Race 列水平拆分,您需要告诉geom 进行拆分。这就是ggplot 术语中所说的“躲避”。在这种情况下,这应该有效:

ggplot(Master_Correlation, aes(x=`Explanatory Variable`, y=`Correlation to Hours Worked`, fill= Race)) +
    geom_col(position=position_dodge())

【讨论】:

【参考方案2】:

您缺少条形的原因是您没有分配几何图形。

library(ggplot2)

gggplot(Master_Correlation, aes(x=Explanatory.Variable, y= Correlation.to.Hours.Worked, 
                               fill= Race)) +
  geom_col()

数据:

structure(list(Correlation.to.Hours.Worked = c(-0.0045128223, 
0.0323171103, -0.1504050423, -0.2511609551, -0.038399312, -0.0571333231, 
0.0266860952, 0.085989946, 0.1392112264, 0.0273427376, -0.0207199322, 
-0.00083698, -0.097906582, -0.1678122716, -0.0584001709, -0.0342631733, 
0.0585449347, 0.0987732746, 0.1461246589, 0.0348695563, -0.0208368904, 
0.0174493239, -0.1635641693, -0.2438919429, -0.0076820903, -0.0337691135, 
0.0376874627, 0.0841707245, 0.1515369812, 0.0637695725, -0.0274184696, 
0.0159808674, -0.1081184916, -0.1676908858, -0.0576837324, -0.0608660949, 
-0.0007628678, 0.1181154823, 0.1467355792, 0.0786165669, -0.0106969158, 
0.0241509708, -0.137043545, -0.1869553011, -0.0391851362, -0.0134763887, 
0.0544616829, 0.0602647172, 0.2082363589, 0.1210709059), Explanatory.Variable = c("                   forborn", 
"                  numchild", "fmale numchild interaction", "                    female", 
"                        hs", "                   somecol", "                   college", 
"                  advanced", "                        rw", "                 rw squared", 
"                    forborn", "                   numchild", 
"female numchild interaction", "                     female", 
"                         hs", "                    somecol", 
"                    college", "                   advanced", 
"                         rw", "                 rw squared", 
"                    forborn", "                   numchild", 
"female numchild interaction", "                     female", 
"                         hs", "                    somecol", 
"                    college", "                   advanced", 
"                         rw", "                 rw squared", 
"                    forborn", "                   numchild", 
"female numchild interaction", "                     female", 
"                         hs", "                    somecol", 
"                    college", "                   advanced", 
"                         rw", "                 rw squared", 
"                    forborn", "                   numchild", 
"female numchild interaction", "                     female", 
"                         hs", "                    somecol", 
"                    college", "                   advanced", 
"                         rw", "                 rw squared"), 
    Race = c("white", "white", "white", "white", "white", "white", 
    "white", "white", "white", "white", "black", "black", "black", 
    "black", "black", "black", "black", "black", "black", "black", 
    "hispanic", "hispanic", "hispanic", "hispanic", "hispanic", 
    "hispanic", "hispanic", "hispanic", "hispanic", "hispanic", 
    "asian", "asian", "asian", "asian", "asian", "asian", "asian", 
    "asian", "asian", "asian", "other", "other", "other", "other", 
    "other", "other", "other", "other", "other", "other")), class = "data.frame", row.names = c("1:", 
"2:", "3:", "4:", "5:", "6:", "7:", "8:", "9:", "10:", "11:", 
"12:", "13:", "14:", "15:", "16:", "17:", "18:", "19:", "20:", 
"21:", "22:", "23:", "24:", "25:", "26:", "27:", "28:", "29:", 
"30:", "31:", "32:", "33:", "34:", "35:", "36:", "37:", "38:", 
"39:", "40:", "41:", "42:", "43:", "44:", "45:", "46:", "47:", 
"48:", "49:", "50:"))

【讨论】:

以上是关于Ggplot条形图不显示任何列[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

ggplot:条形图面板

在R ggplot2中将条形图绘制到地图上

Matplotlib 条形图不接受数组作为高度参数

R语言使用ggplot2可视化堆叠条形图,并在堆叠条形图上显示数据值实战

用均值条形图及其标准差 ggplot2 总结数据框

R语言ggplot2可视化:计算dataframe中每个数据列缺失值的个数使用堆叠的条形图(Stacked Barplot)可视化每个数据列的缺失值的情况(自定义堆叠条形图的形式)