0

I'm trying to transform my primary axis to make all the data readable in relation to the secondary axis. Here's the code I'm using:

ggplot(data = NOAARainfallAirTemp, aes(x = Date)) + 
  geom_smooth(aes(y = Temp, color ="Temperature"), method = "loess", span = 0.3, fill = "blue") + 
  geom_point(aes(y = Temp, color = "Temperature"), alpha = 0.3) + 
  geom_smooth(aes(y = PRCP*6, color = "Precipitation"), method = "loess", span = 0.3, fill = "orange") + 
  geom_point(aes(y = PRCP*6, color = "Precipitation"), alpha = 0.3) + 
  scale_x_date(expand = c(0.001,0.001), date_breaks = "3 months", date_labels = "%b%Y", date_minor_breaks = "1 month")  + 
  scale_y_continuous(expand = c(0.001,0.001), sec.axis = sec_axis(~./6, name = "Precipitation")) + 
  ylab("Temperature (°C)") + 
  ggtitle("Diagnostics of NOAA Data") + 
  theme(axis.text.x=element_text(angle = 90, hjust = 0), plot.title = element_text(hjust = 0.5)) + 
  guides(shape = guide_legend(override.aes = list(shape = 15)), color = guide_legend(override.aes = list(fill = NA))) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_line(colour = "grey"),panel.background = element_blank(), axis.line = element_line(colour = "black"))

The code makes this graph: enter image description here

As you can see, it's hard to make out trends, especially for the precipitation data. Is there a way to "spread out" the data for the primary axis or is there a better way of going about this?

Any help will be greatly appreciated!

godot
  • 1,550
  • 16
  • 33
SecretBeach
  • 197
  • 1
  • 12
  • Please reformat your code so that each ggplot function is on a separate line, which will make it much easier to read. These SO answers ([here](https://stackoverflow.com/a/45777841/496488) and [here](https://stackoverflow.com/a/48197493/496488)) may be helpful. – eipi10 Nov 29 '18 at 19:49
  • 1
    Separate from the issue your having, you can shorten your code considerably by converting your data to "long" format, which will then require only one call to `geom_smooth` and one to `geom_point`. Also, you can use `data = NOAARainfallAirTemp` in the initial ggplot call and remove `data = NOAARainfallAirTemp` from all of the geoms. – eipi10 Nov 29 '18 at 19:56
  • @eipi10 it's been edited! – SecretBeach Nov 29 '18 at 19:56
  • 2
    You won't gain much from spreading the principal axes. I don't know what you are trying to do but I feel you would benefit from dividing your data into 2 graphs one for each factor using for example facets (with "free_y" for the `scales` argument) – godot Nov 29 '18 at 20:09
  • 1
    To you show you how to shorten your code, here's an example with a built-in data frame. The second plot converts the data from "wide" to "long" on the fly. Can't format code in comments, so you'll need to paste into a script to view easily: `library(tidyverse)`; `ggplot(mtcars, aes(carb)) + geom_smooth(aes(y=mpg, colour="mpg")) + geom_point(aes(y=mpg, colour="mpg")) + geom_smooth(aes(y=hp, colour="hp")) + geom_point(aes(y=hp, colour="hp"))`; `ggplot(mtcars %>% gather(key, value, mpg, hp), aes(carb, value, colour=key, fill=key)) + geom_smooth() + geom_point()` – eipi10 Nov 29 '18 at 20:11
  • @godot, unfortunately, I need the two variables to be on one graph. I'm trying to manipulate either axis so that I can see more a trend. I've tried multipyling it and then dividing in the sec.axis() function, but it doesn't seem to work. – SecretBeach Nov 29 '18 at 20:19
  • Thanks @eipi10, I'll definitely look into that! – SecretBeach Nov 29 '18 at 20:19

0 Answers0