0

I have a data set like this

df<- structure(list(X = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 
3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L), .Label = c("Control1", 
"Control2", "Control3", "Control4", "Control5", "Control6", "Control7"
), class = "factor"), name = structure(c(3L, 2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 
3L, 2L, 1L), .Label = c("Cond1", "Cond2", "Cond3"), class = "factor"), 
    Value = c(1.1, 1.7, -0.7, 0.8, 1.9, -0.71, 1.6, 1.6, -0.5, 
    1.9, 1.8, -0.6, 1.7, 1.5, -0.9, 1.3, 1.5, -0.7, 1.2, 1.6, 
    -0.5), Signifcance = c(1.06e-18, 9.4e-20, 9e-04, 5.7e-10, 
    3.02e-50, 8.01e-08, 1.4e-19, 8.6e-83, 3.2e-07, 6.8e-15, 1.2e-92, 
    1.7e-09, 6.2e-15, 3.4e-68, 1.4e-09, 6.9e-15, 2.3e-48, 4.3e-12, 
    1.1e-21, 1.7e-141, 1.4e-08)), class = "data.frame", row.names = c(NA, 
-21L))

I have been trying to figure out how to plot the data on top of each other based on the first column

If you look at the column X, there are Control1 (3 times) Control2(3 times) etc. In each control I have 3 conditions I wanted to have 3 lines corresponding to each cond across all Controls versus one axis and then one line for the significanc value for each with another color.

I checked here and I found this solution but I cannot figure out how to use it for this data How can I plot with 2 different y-axes?

any help is appreciated

nik
  • 2,500
  • 5
  • 21
  • 48

1 Answers1

1

I'm not entirely sure how you want the resulting plot to look like, but is this going in the direction you intended?

mat = matrix(c(1,1,1,2,3,4), 3, 2, byrow = F)
layout(mat)

plot(df[df$name == "Cond1",]$Value, col="red", type="l", ylim=c(min(df$Value), max(df$Value)), ylab="Value", main="Values")
lines(df[df$name == "Cond2",]$Value, col="blue", type="l")
lines(df[df$name == "Cond3",]$Value, col="green", type="l")
legend("right", legend = c("Cond1","Cond2","Cond3"), lwd = c(1,1,1), col=c("red","blue","green"))


plot(df[df$name == "Cond1",]$Signifcance, col="red", lty=2, type="l", ylab="Sign.", main="Significance")
plot(df[df$name == "Cond2",]$Signifcance, col="blue", lty=2, type="l",ylab="Sign.")
plot(df[df$name == "Cond3",]$Signifcance, col="green", lty=2, type="l",ylab="Sign.")
legend("right", legend = c("Cond1","Cond2","Cond3"), lty = 2, col=c("red","blue","green"))

Or with the dev-version of ggplot2, you can easily split the significance values in 3 plots with different y-axis.

devtools::install_github("tidyverse/ggplot2")

library(ggplot2)
library(gridExtra)

plot1 <- ggplot(df, aes(X, Value, group=name, color=name)) + 
  geom_line() +
  geom_point() + 
  ggtitle("Values")

plot2 <- ggplot(df, aes(X, Signifcance, group=name, color=name)) + 
  geom_line() + 
  facet_grid(rows=vars(name), scales = "free_y")+ 
  ggtitle("Signifcance")

grid.arrange(plot1, plot2, ncol=2)
SeGa
  • 9,454
  • 3
  • 31
  • 70
  • I think you are doing the right way but I cannot understand why you plot 3 conditions and their significant on the same axis ? they are very different in magnitude , also the labeling in the x-axic does not reflact the Control . – nik Jul 11 '18 at 16:00
  • Indeed, I just realised the same thing. Why not divide the plot, so that you have values on the left and Significance on the right? That would make the plots more readable and also easier to build? – SeGa Jul 11 '18 at 16:03
  • @ SeGa is there another way that I could plot the significant so that I could show all the points or somewhat that it can be seen? also , would it be possble to add legend? and Yes definetly the second approach might be more of use :-) thanks – nik Jul 11 '18 at 16:09
  • Just edited the answer. The plots for the significance are splitted and there is a ggplot2 version aswell. – SeGa Jul 11 '18 at 20:16
  • I get this error `Error in facet_grid(rows = vars(name), scales = "free_y") : unused argument (rows = vars(name))` – nik Jul 11 '18 at 20:49
  • Oh I think you need the **dev**-version of ggplot2 for that. – SeGa Jul 11 '18 at 20:51
  • what is the dev-version? is there a way to get a normal one ? if no, how can I get the other one installed ? – nik Jul 11 '18 at 20:52
  • I just added the code-snippet to install the dev (developer) version. – SeGa Jul 11 '18 at 21:06
  • I accepted and liked your answer ! Thanks for your time – nik Jul 12 '18 at 02:04