0

Here is my question, I have a data like this

  A B C D 

a 24 1 2 3 

b 26 2 3 1

c 25 3 1 2

Now I would like to plot A in a Y axis (0 to 30) and B~D in another Y axis (0 to 5) in one graph. Also, I want a, b, c row has a line to link them together (lets say a, b, c represents a mouse ID). Could anyone come up with ideas on how to do it? I prefer using R. Thanks in advance!

Xin Zhou
  • 1
  • 1
  • I'm not sure what you mean by a,b,c row has a line to link them together, but I can adjust the code below if you expand on your meaning – s_scolary Nov 03 '15 at 16:25
  • Try to draw an image, maybe that'll explain your needs better – m02ph3u5 Nov 03 '15 at 16:33
  • In a double-axis plot, how does the reader/viewer know which axis should be used with which datapoint? – Heroka Nov 03 '15 at 16:40
  • Hi @Heroka, I was thinking about adding a legend to explain it. – Xin Zhou Nov 04 '15 at 16:36
  • Hi @Ranalyst, what I mean is something like a x/y plot where you have lines to link all data points belongs to a or b or c, in this case I would like a line links aA, aB, aC, aD, another line link bA, bB, bC, bD, etc. – Xin Zhou Nov 04 '15 at 16:42
  • you can connect points with a line by adding `type="l"` or if you want to keep the lined and points `type="b"` to your plot functions or within the points function `points(1:3,data$C,pch = 19,col = 3,type="b")` – s_scolary Nov 04 '15 at 18:51

2 Answers2

3
# create some data
data = as.data.frame(list(A = c(24,26,25),
                     B = c(1,2,3),
                     C = c(2,3,1),
                     D = c(3,1,2)))

# adjust your margins to allow room for your second axis
par(mar=c(5, 4, 4, 4) + 0.1)
# create your first plot
plot(1:3,data$A,pch = 19,ylab = "1st ylab",xlab="index")

# set par to new so you dont' overwrite your current plot
par(new=T)
# set axes = F, set your ylim and remove your labels
plot(1:3,data$B,ylim = c(0,5), pch = 19, col = 2,
     xlab="", ylab="",axes = F)

# add your points
points(1:3,data$C,pch = 19,col = 3)
points(1:3,data$D, pch = 19,col = 4)

# set the placement for your axis and add text
axis(4, ylim=c(0,5))
mtext("2nd ylab",side=4,line=2.5)

enter image description here

s_scolary
  • 1,361
  • 10
  • 21
  • Thanks, this isn't exactly what I meant. Sorry for the confusing, say that we has a mouse body weight is 25g, I would like to show weight changes in percentages (, 1% in day 1, 2% in day 4 etc.), I understand that I can set all mouse to 100% at day 0, but I would also like to include their initial body weight, which is different from each mouse. Does this makes sense? The eaiser way is to plot their bodyweight day by day, but I was just wondering if there is alternatives like this in R. – Xin Zhou Nov 04 '15 at 16:35
  • I see what you mean. Have you considered just adding the initial mouse mass to a legend? That way you can add your weight change % to the plot. If you had the initial weights in a legend and only plotted the %'s, your graph is more likely to be standardized (eg. all values +/-10%) versus if you had two mice with vastly different weights (eg mouse 1 = 20 g and mouse 2 = 50 g) – s_scolary Nov 04 '15 at 18:57
  • @Ranalysis, that also works, I actually did what exactly you said originally, just posted this to see if there is other method. Thank you. – Xin Zhou Nov 05 '15 at 16:01
1

I greatly prefer using ggplot2 for plotting. Sadly, ggplot2 does not support this for philosophical reasons.

I would like to propose an alternative which uses facets, i.e. subplots. Note that to be able to plot the data using ggplot2, we need to change the data structure. We do this using gather from the tidyr package. In addition, I use the programming style as defined in dplyr (which uses piping a lot):

library(ggplot2)
library(dplyr)
library(tidyr)

df = data.frame(A = c(24, 26, 25), B = 1:3, C = c(2, 3, 1), D = c(3, 1, 2))
plot_data = df %>% mutate(x_value = rownames(df)) %>% gather(variable, value, -x_value)
ggplot(plot_data) + geom_line(aes(x = x_value, y = value, group = variable)) + 
                    facet_wrap(~ variable, scales = 'free_y')

enter image description here

Here, each subplot has it's own y-axis.

Community
  • 1
  • 1
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • Hi Paul, thanks. this is. lets say, plotting each point within a Column, what I need is plotting each point in a row. That is to say, how to put 25, 1, 2, 3 in a graph where 25 belongs to one axis and 1,2,3 belongs to another. – Xin Zhou Nov 04 '15 at 16:39