Sarah
Sarah

Reputation: 463

Adding title and another line to line graph using ggplot2

I got help from another user on how to create these plots (thank you!):

test <- data.frame("Site_No" = c("01370", "01332", "01442"),"0.99" = c(12, 15, 18), "0.98" = c(14, 
15, 18), "0.90" = c(7, 22, 30), ".80" = c(3,2,1), ".75" = c(1, 6, 8), ".70" = c(5,6,9), ".60" = c(15,6,19), ".50" = c(5,6,9), ".40" = c(9,16,20), ".30" = c(1, 15, 3), ".25" = c(5,16,19), ".20" = c(5,1,20), ".10" = c(11,12,13), ".05" = c(15,16,28), "0.02" = c(22,20,12), ".01" = c(3,26,29))

dt <- as.data.table(test)

melted <- data.table::melt(dt, measure = c("X0.99","X0.98","X0.90"))

for (i in unique(melted$Site_No)){
    dev.new()
    print(ggplot2::ggplot(data = melted[Site_No == i,], mapping = aes(x = variable, y = value, group 
    = Site_No)) +
    ggplot2::geom_line())
} 

I just have a few questions for some additions

1) I would like to add a title to each of these graphs with the Site_No. I tried adding title = Site_no to the code, but it didn't work.

2) I would like to add another line to this graph that has this data (a different color than the other line):

test2 <- data.frame("Site_No" = c("01370", "01332", "01442"),"0.99" = c(19, 36, 22), "0.98" = c(19, 
10, 28), "0.90" = c(2, 6, 8))

I tried copying the same code to add the other line, but it didn't work.

3) I would like to have each of these 3 plots save to my local directory automatically. So I don't have to do it individually for each plot (I am running 100 plots in reality, not 3).

Thank you so much for your help :)

Upvotes: 1

Views: 815

Answers (1)

dc37
dc37

Reputation: 16178

For your question 1), to add a title, you can use ggtitle in your function.

For the question 2), a possible solution is to bind together your both dataframe.

library(data.table)
melted2 <- melt(setDT(test2), measure = c("X0.99","X0.98","X0.90"))

library(dplyr)
DF <- left_join(melted, melted2, by = c("Site_No","variable")) 

DF <- melt(setDT(DF), measure = c("value.x","value.y"), variable.name = "Test",value.name = "Value")

    Site_No variable    Test Value
 1:   01370    X0.99 value.x    12
 2:   01332    X0.99 value.x    15
 3:   01442    X0.99 value.x    18
 4:   01370    X0.98 value.x    14
 5:   01332    X0.98 value.x    15
 6:   01442    X0.98 value.x    18
 7:   01370    X0.90 value.x     7
 8:   01332    X0.90 value.x    22
 9:   01442    X0.90 value.x    30
10:   01370    X0.99 value.y    19
11:   01332    X0.99 value.y    36
12:   01442    X0.99 value.y    22
13:   01370    X0.98 value.y    19
14:   01332    X0.98 value.y    10
15:   01442    X0.98 value.y    28
16:   01370    X0.90 value.y     2
17:   01332    X0.90 value.y     6
18:   01442    X0.90 value.y     8

Then, to add a second line to your graph, you can modify group in the aes and add the color argument.

So, your function should look like this:

for (i in unique(DF$Site_No)){
  dev.new()
  print(ggplot2::ggplot(data = DF[Site_No == i,], mapping = aes(x = variable, y = Value, group 
                                                                    = Test)) +
          ggplot2::geom_line(aes(color = Test)) +
          ggplot2::scale_color_discrete(labels = c("test1","test2"))+
          ggplot2::ggtitle(paste("Title:", i)))
} 

For your question 3), you can use ggsave to directly save the graph into your current directory.

library(ggplot2)

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("test1","test2"))+
  ggtitle(paste("Title:", i))

  ggsave(filename = paste0("Site_",i,".png"), plot = graph, device = "png", width  = 5, height = 5, units = "in")
}

here an example of the graph saved: enter image description here


EDIT: With more x values: continuous vs discrete plot

You mentioned you have 18 x values representing some percentiles and you would like them to be nicely display on your graph (they are confounded right now).

One way is to keep those values discrete and simply reduce the size of the x axis text in theme.

Here, the preparation of the datatable based on your new example:

library(data.table)

melted <-  melt(setDT(test), measure = list(grep("X",colnames(test))))
melted2 <- melt(setDT(test2), measure = list(grep("X",colnames(test2))))

DF <- left_join(melted, melted2, by = c("Site_No","variable")) 
DF <- melt(setDT(DF), measure = c("value.x","value.y"), variable.name = "Test",value.name = "Value")
DF$variable <- gsub("X\\.","X0\\.",DF$variable)

For the plot, you can get:

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("test1","test2"))+
    ggtitle(paste("Title:", i))+
    theme(axis.text.x = element_text(angle = 90, size = 10, vjust = 0.5))

  ggsave(filename = paste0("Site_",i,".png"), plot = graph, device = "png", width  = 5, height = 5, units = "in")

}

Which gives you the following graph: enter image description here

An another possibilty is to represent your data on a continuous scale and arrange the labeling to show a little bit less of text:

DF2 <- DF %>% mutate(variable = as.numeric(gsub("X","",variable)))
setDT(DF2)
for (i in unique(DF2$Site_No)){
  graph <- ggplot(data = DF2[Site_No == i,], mapping = aes(x = variable, y = Value, group 
                                                          = Test)) +
    geom_line(aes(color = Test)) +
    scale_color_discrete(labels = c("test1","test2"))+
    scale_x_continuous(breaks = seq(0,1,by = 0.1))+
    ggtitle(paste("Title:", i))

  ggsave(filename = paste0("Site_",i,"_conti_.png"), plot = graph, device = "png", width  = 5, height = 5, units = "in")
}

Which gives this kind of graph: enter image description here

Finally, a third possibility is to add a scale to ggsave:

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("test1","test2"))+
    ggtitle(paste("Title:", i))

  ggsave(filename = paste0("Site_",i,".png"), plot = graph, device = "png", width  = 5, height = 5, units = "in", scale = 2)  
}

enter image description here

You can also mix those solutions together and get some continuous scale with rotating labeling fro example. It's up to you.

Does it answer your question ?

Upvotes: 1

Related Questions