Home > Software design >  Ggplot bar plot not displaying any columns
Ggplot bar plot not displaying any columns

Time:12-04

I am a beginner to R and have the following correlation table (this is it in its entirety):

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

Notice that I have 5 races, and each race has a correlation observation associated with 10 explanatory variable. My objective is to make a bar plot where the x-axis is the names of the 10 explanatory variables that has 5 bars corresponding to the observation of each race. I tried the following but what prints is a blank graph. Any help (especially feedback telling me why this didn't work so I can get better at coding) is greatly appreicated!

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

enter image description here

Data:

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"))

CodePudding user response:

OP, you're almost there. You've setup the plot area and environment with all the necessary mapping (i.e. what is x, y, and what corresponds to the fill color). The problem is that you haven't told ggplot that you want to draw something!. In ggplot terms, that means specifying a geom.

In this case, you have y and x values, so you want a "column" plot (strictly-speaking), and not a "bar" plot (which needs a list of x values). Just add geom_col() to your code. Try this:

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

That will draw the columns, but the default way to display columns of different color that share the same x value is to "stack" them on top of each other. In order to make sure they split horizontally according to the Race column in your data, you need to tell the geom to split. This is what is referred to as "dodging" in the ggplot lingo. In that case, this should work:

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

CodePudding user response:

The reason you are missing the bars is that you do not have assigned a geom.

library(ggplot2)

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

enter image description here

data:

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:"))
  • Related