why does my graph have no data?

I'm trying to make a simple line graph (graphing 3 different y variables) using one x-axis. I'm not receiving any error messages, but no data appears on my plot, though the axis/titles/legends do.

My code is below:

JSWC_Summary_TC_Sep16 <- read_excel("~/JSWC_Results/JSWC_Summary_TC_Sep16.xlsx", 
                                   sheet = "Man")
# View(JSWC_Summary_TC_Sep16)
# Data$N_Rate<- factor(Data$N_Rate, levels=c("0", "40", "80", "120", "160", "200", "240","280"))
Data$Scenario<- factor(Data$Scenario, levels=c("FALL", "SPRING","FALL+SPRING"))
p <-ggplot(Data, aes(x=Scenario, linetype="dashed")) +
  geom_line(aes(y=Crop_Yield, color='Avg Crop Yield')) +
  geom_line(aes(y=Dry_Crop_Yield, color='5 Driest Years')) +
  geom_line(aes(y=Wet_Crop_Yield, color='5 Wettest Years')) 

Here is an example data set:

  ~Location, ~N_Rate,     ~Scenario, ~Nitrate_kg.ha, ~Nitrate_ppm, ~Crop_Yield, ~Dry_Nitrate_kg.ha, ~Dry_Nitrate_ppm, ~Dry_Crop_Yield, ~Wet_Nitrate_kg.ha, ~Wet_Nitrate_ppm, ~Wet_Crop_Yield,
     "SHEB",   "MAN",        "FALL",         51.548,      12.8325,      41.486,              11.27,           74.488,          11.714,             93.113,           79.032,         116.218,
     "SHEB",   "MAN",      "SPRING",         33.009,        7.504,      25.412,               6.24,           48.372,           6.344,             62.737,           33.806,         113.826,
     "SHEB",   "MAN", "FALL+SPRING",         20.278,       3.6815,       13.56,              2.202,           29.568,             2.3,            12.8775,            1.042,           28.34

Can you please share a small part of the data set in a copy-paste friendly format?

In case you don't know how to do it, there are many options, which include:

  1. If you have stored the data set in some R object, dput function is very handy.

  2. In case the data set is in a spreadsheet, check out the datapasta package. Take a look at this link.

@andresrcs I made an edit to the original post - is this what you had in mind?

Thanks for any help!

Add group=1 inside aes like this: ggplot(Data, aes(x=Scenario, group=1)). When the x-axis is categorical, ggplot treats each x-value as a separate group. With only one point per group, there aren't multiple points within a group to connect with line segments. group=1 (or group="whatever") overrides the default grouping. (If you add, for example, geom_point(aes(y=Crop_Yield, color='Avg Crop Yield')) to your plot code, you'll see the points appear.)

Also, linetype="dashed" doesn't do anything when added to the main ggplot call. You can hardcode linetypes inside geom_line by adding linetype="dashed" outside of aes().

ggplot works most effectively with data in long format, so an easier way to do this plot is to convert your data from wide to long format, which I've done below with the gather function. With this approach, you need only one call to geom_line and the legend is automatically created. Note that I added group=key to once again override the default grouping. I've also removed the label underscores with a call to scale_colour_discrete.

Data %>% 
  gather(key, value, Crop_Yield, Dry_Crop_Yield, Wet_Crop_Yield) %>% 
  ggplot(aes(Scenario, value, colour=key, group=key)) +
    geom_line(linetype="dashed") +
    scale_colour_discrete(labels=function(x) gsub("_", " ", x))

Thank you! This was extremely helpful!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.