Search code examples
rggplot2graphdata-visualizationgeom-area

Stacked graph not showing on chart. R ggplot geom_area


I am trying to show a stacked graph using an area chart. However, after i input the variables for x and y and the fill data, nothing shows on the graph.

ggplot()+
geom_area(data=provinces,aes(x=variable,y=value,fill=Province.State))
Province.State variable value
         Hubei 01.22.20   444
     Guangdong 01.22.20    26
         Henan 01.22.20     5
      Zhejiang 01.22.20    10
         Hunan 01.22.20     4
         Anhui 01.22.20     1
         Macau 01.27.20     6
         Tibet 01.27.20     0
         Hubei 01.28.20  3554
     Guangdong 01.28.20   207
         Henan 01.28.20   168
      Zhejiang 01.28.20   173
         Hunan 01.28.20   143
         Anhui 01.28.20   106
       Jiangxi 01.28.20   109

output


Solution

  • variable is not continuous; without transforming those strings into something numeric, it's a factor with three discrete values. Either use geom_bar(stat='identity'), to keep variable as a factor, or parse the dates:

    ggplot(provinces,
      aes(x=as.Date(variable, format='%m.%d.%y'),
      y=value,
      fill=Province.State)) + geom_area()
    

    (After parsing the data with:

    provinces <- read.table(header=TRUE, text='Province.State variable value
             Hubei 01.22.20   444
         Guangdong 01.22.20    26
             Henan 01.22.20     5
          Zhejiang 01.22.20    10
             Hunan 01.22.20     4
             Anhui 01.22.20     1
             Macau 01.27.20     6
             Tibet 01.27.20     0
             Hubei 01.28.20  3554
         Guangdong 01.28.20   207
             Henan 01.28.20   168
          Zhejiang 01.28.20   173
             Hunan 01.28.20   143
             Anhui 01.28.20   106
           Jiangxi 01.28.20   109
    ')
    

    .)