Ggplot 条形图不显示任何列
Ggplot bar plot not displaying any columns
我是 R 的初学者,具有以下相关性 table(完整内容):
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 个解释变量相关的相关观察。我的 objective 是制作一个条形图,其中 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"))
OP,你快到了。您已经使用所有必要的映射设置绘图区域和环境(即什么是 x
、y
,以及什么对应于 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())
您缺少条形图的原因是您没有分配 geom。
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:"))
我是 R 的初学者,具有以下相关性 table(完整内容):
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 个解释变量相关的相关观察。我的 objective 是制作一个条形图,其中 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"))
OP,你快到了。您已经使用所有必要的映射设置绘图区域和环境(即什么是 x
、y
,以及什么对应于 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())
您缺少条形图的原因是您没有分配 geom。
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:"))