Reputation: 463
I have code that looks like this currently (from the help of many of you on this website, so thank you!)
#my data
DF <- data.table("Site_No" = c(123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123),
"variable" =
c(1,1,2,2,5,5,10,10,20,20,25,25,30,30,40,40,50,50,60,60,70,70,75,75,80,80,90,90,95,95,98,98,99,99),
"Test" = c("value.x", "value.y", "value.x", "value.y", "value.x", "value.y","value.x", "value.y",
"value.x", "value.y", "value.x", "value.y","value.x", "value.y", "value.x", "value.y", "value.x",
"value.y","value.x", "value.y", "value.x", "value.y", "value.x", "value.y","value.x", "value.y",
"value.x", "value.y","value.x", "value.y","value.x", "value.y","value.x", "value.y"),
"Value" = c(800, 700, 600, 580, 570, 560, 520, 500, 430, 415, 412, 402, 380, 370, 366, 300, 290, 280,
270, 260, 250, 240, 230, 220, 210, 200, 180, 160, 150, 130, 100, 95, 67, 40))
#create plots
for (i in unique(DF$Site_No)){
graph <- ggplot(data = DF[Site_No == i,], mapping = aes(x = variable, y = Value, group
= Test)) +
geom_line(aes(color = Test)) +
scale_color_discrete(labels = c("annual","winter"))+
scale_x_discrete(name ="Percent Chance Flow Exceeded",
limits=c("1", "10", "20", "30", "40", "50", "60", "70", "80", "90", "99"))+
labs(x = "Percent Chance Flow Exceeded", y = "Streamflow, In cubic feet per second")+
ggtitle(paste(i))
ggsave(filename = paste0("Site_",i,".png"), plot = graph, device = "png", width = 5, height = 5, units = "in")
}
Here are my requests that I would like to be changed please! 1) Make the x-axis be spaced like this: 0.5, 2, 5, 10, 20, 30, 40, 60, 70, 80, 90, 95, 98, 99
2) Make both of the lines (annual and winter) be in log scale, but keep the y-axis NOT in log scale!
Thank you so much for your help :)!!!
Upvotes: 0
Views: 353
Reputation: 23727
I am using data.frame, so I can use regular tidyverse piping. You need to transform your data before plotting. I'd do this before passing your data to the for loop. I removed this loop in this example, because your data only contained one ID.
In order to show axis breaks at round 'readable' numbers, you need the log values of those round numbers for the axis breaks, and then for the labels use the exponent to the log... if this makes sense. See code.
library(tidyverse)
DF <- data.frame("Site_No" = c(123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123),
"variable" =
c(1,1,2,2,5,5,10,10,20,20,25,25,30,30,40,40,50,50,60,60,70,70,75,75,80,80,90,90,95,95,98,98,99,99),
"Test" = c("value.x", "value.y", "value.x", "value.y", "value.x", "value.y","value.x", "value.y",
"value.x", "value.y", "value.x", "value.y","value.x", "value.y", "value.x", "value.y", "value.x",
"value.y","value.x", "value.y", "value.x", "value.y", "value.x", "value.y","value.x", "value.y",
"value.x", "value.y","value.x", "value.y","value.x", "value.y","value.x", "value.y"),
"Value" = c(800, 700, 600, 580, 570, 560, 520, 500, 430, 415, 412, 402, 380, 370, 366, 300, 290, 280,
270, 260, 250, 240, 230, 220, 210, 200, 180, 160, 150, 130, 100, 95, 67, 40))
DF %>% mutate(logval = log(Value)) %>%
ggplot(mapping = aes(x = variable, y = logval, group = Test)) +
geom_line(aes(color = Test)) +
scale_color_discrete(labels = c("annual","winter")) +
scale_y_continuous(breaks = log(seq(100,800,100)), labels = exp(log(seq(100,800,100)))) +
scale_x_discrete(name ="Percent Chance Flow Exceeded",
limits=c( 0.5, 2, 5, 10, 20, 30, 40, 60, 70, 80, 90, 95, 98, 99)) +
labs(x = "Percent Chance Flow Exceeded", y = "Streamflow, In cubic feet per second")
Created on 2020-04-22 by the reprex package (v0.3.0)
Upvotes: 1