0

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

lams
  • 352
  • 1
  • 10
  • You have backtick-quoted the `geom_area()` bit which seems wrong to me. Also, please don't post data [as images](https://meta.stackoverflow.com/a/285557/11374827). – teunbrand Sep 12 '21 at 21:22
  • Welcome lams, an example that we can reproduce would help, the reprex package is great for that, and so is this post : https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – moodymudskipper Sep 12 '21 at 21:29
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 12 '21 at 22:25
  • I've added sample data, i am not sure exactly why nothing is showing on the graph – lams Sep 13 '21 at 00:20

1 Answers1

0

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

.)

Joe
  • 29,416
  • 12
  • 68
  • 88