0

Sorry in advance if I'm not very clear in the explanation of my problem (I'm very new to R) but I am trying to graph 2 variables (Average cereal yield, Average land area change) into the same area chart to show the difference between cereal yield growth and land area change between a date range but I can't figure out how to get it to work the way I want it to. Here's a picture of my initial dataset with respective variables:

  
year    meancereal   meanland
<chr>    <dbl>        <dbl>
1961    100.0000    100.0000        
1962    107.6528    100.9864        
1963    108.3420    101.4125        
1964    109.5492    104.1026        
1965    111.8808    105.2984        
1966    110.3679    106.5562        
1967    117.0725    109.7375        
1968    117.5337    112.8811        
1969    120.2902    114.9961        
1970    120.5337    114.4149    

I've tried converting to a longer dataset so that instead of having 2 separate columns I just have one for Averages and one for Values:

 year      Avg        value
<chr>      <chr>      <dbl>
1961    meancereal  100.0000        
1961    meanland    100.0000        
1962    meancereal  107.6528        
1962    meanland    100.9864        
1963    meancereal  108.3420        
1963    meanland    101.4125        
1964    meancereal  109.5492        
1964    meanland    104.1026        
1965    meancereal  111.8808        
1965    meanland    105.2984

This is the code I've written so far:

landchangeC<-land_use %>%
  filter(year>1960,year<2015)%>%
  group_by(year) %>%
  summarise( meancereal= mean(cereal_yield_index, na.rm=TRUE),
             meanland=mean(land_area_change, na.rm=TRUE))


landchangeC<-landchangeC%>%
  pivot_longer(meancereal:meanland, names_to="Avg", values_to="value")


landchangeC%>%
  ggplot(aes(year, value, color=Avg))+
  geom_line()+
  geom_area()

After I run the code It just shows a blank graph:

enter image description here

Any help is much appreciated!

MM97
  • 1
  • 1
  • 2
    Please do not post an image of code/data/errors: it cannot be copied or searched (SEO), it breaks screen-readers, and it may not fit well on some mobile devices. Ref: https://meta.stackoverflow.com/a/285557 (and https://xkcd.com/2116/). Please just include the code, console output, or data (e.g., `dput(head(x))` or `data.frame(...)`) directly. – r2evans Sep 23 '20 at 03:29
  • (I suggested an edit that includes the output of `dput` on what I think is your data. Please confirm it's close-enough. It doesn't have to be the same exact values, but the types and ranges need to be right.) – r2evans Sep 23 '20 at 03:35
  • I don't get your image when I plot your data. When I remove `geom_area()`, it a two-line chart, which seems appropriate given the data. Notes about `geom_area`: (1) it defaults to a y minimum of 0, which completely changes the scale; (2) the `fill=` is defaulting for me to black, you might `color=Avg, fill=Avg`; (3) by default, it is stacking the values, not raw values, so it's a sand-plot. Bottom line, I can't reproduce that plot with that data. – r2evans Sep 23 '20 at 03:39
  • @r2evans, I removed geom_area() so that I was only running geom_line() but the graph is still blank. I also received the message: 'geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?' – MM97 Sep 23 '20 at 04:32
  • 2
    One guess: Your year variable is a character. Try adding the group aes, i.e. `ggplot(aes(year, value, color=Avg, group = Avg))`. – stefan Sep 23 '20 at 05:45
  • @stefan, problem solved! thanks heaps – MM97 Sep 23 '20 at 07:12

0 Answers0