0

I have the following problem: I want to create a plot using ggplot, showing the relationship between two variables (Microplastic quantification in mussels, denoted as MP and Lipofuscinaccumulation denoted as Lip) of different treatment groups and independence of exposure time.

My data look like this: enter image description here

And here is my Code:

ggplot(Catrv_all,aes(Lip,MP,color=treatment))+
  geom_smooth(method="lm", se=FALSE)+
  geom_point(size = 2)+
  theme(legend.position = "bottom")+
  theme(plot.title = element_text(hjust = 0.5))+
  labs(x = "Lipofuscin accumulation [% area]", 
       y = "Microplastic quantification [% area]", 
       title = "Lipofuscin accumulation vs. Microplastic quantification")

The plot looks like this: enter image description here

I recognized that ggplot obviously did not order the values in the correct way for exposure Time because the values disagree (it starts for example not with the value for 0 h).

My question is: how can I tell ggplot to reorder the values for MP and Lip in the right order in terms of exposure Time? Should I create second x-axes? If yes, how can I do that in ggplot?

I saw a lot of discussions in SO, that this is difficult to create a second x/y axes in ggplot, but I don't know how I should visualize my data in another way.

Update for my question: I heed advice of sconfluentus and found a very interesting answer of Ben Bolker in the following post: How can I plot with 2 different y-axes?

I adapted the provided code:

## add extra space to right margin of plot within frame
par(mar=c(5, 4, 4, 6) + 0.1)

## split data set for treatment groups
MP_Ko<-Catrv_all$MP[1:8]
exp<-Catrv_all$expTime[1:8]
Lip_Ko<-Catrv_all$Lip[1:8]

## Plot first set of data and draw its axis
plot(exp, MP_Ko, pch=16, axes=FALSE, xlab="", ylab="", 
     type="b",col="black", main="Microplastic quantification vs. Lipofuscin accumulation in Controls")
axis(2,col="black",las=1)  ## las=1 makes horizontal labels
mtext("Microplastic quantification [% area]",side=2,line=2.5)
box()

## Allow a second plot on the same graph
par(new=TRUE)

## Plot the second plot and put axis scale on right
plot(exp,Lip_Ko, pch=15,  xlab="", ylab="", 
     axes=FALSE, type="b", col="red")
## a little farther out (line=4) to make room for labels
mtext("Lipofuscin accumulation [% area]",side=4,col="red",line=4) 
axis(4, col="red",col.axis="red",las=1)

## Draw the time axis
axis(1,pretty(range(Catrv_all$expTime, 672)))
mtext("Time (Hours)",side=1,col="black",line=2.5)

## Add Legend
legend("topright",legend=c("Microplastic quantification","Lipofuscin accumulation"),
       text.col=c("black","red"),pch=c(16,15),col=c("black","red"))

... and got the following plot: enter image description here

Time consuming, but this approach was very helpful.

Apple
  • 1
  • 1
  • Please paste your data as text, not image. – zx8754 Oct 18 '17 at 10:58
  • 3
    What exactly do you mean when you say "order the values in the correct way for exposure Time"? Your plot does not include the exposure time in any way. Also you could use `dput(Catrv_all)` and copy/paste the output to make your data available for the community. – f.lechleitner Oct 18 '17 at 11:00
  • 2
    The variables you assigned to x / y-axis (MP & Lip) appear to be numeric, so it makes sense that the points get ordered according to increasing MP / Lip values. If you want to order by exposure time, do you mean you want to assign expTime to the x-axis instead? Kindly include an illustration of your desired output. That would make it easier for others to understand your requirements. – Z.Lin Oct 18 '17 at 11:10
  • 1
    Once you tackle what @Z.Lin suggests, I'd advise against a second axis. You should `gather` or `melt` the LIP and MP columns and use that to create a variable to use `facet_wrap` to see and compare the two conditions side by side. – Jake Kaupp Oct 18 '17 at 12:04
  • I think that Z.Lin is pointing you in the right direction. You can create two time trends, one for MP and one for Lip each opposing time on the X axis. In which case you would see relative changes over time between the two values based on how those two lines interacted (and you could do a separate graph for each treatment using facet_wrap). Or you can do what you have done, which simply compares point to point with a scatter plot....the trend line is somewhat misleading if there is a time element to these effects which is lost in the scatter plot. – sconfluentus Oct 18 '17 at 14:01

1 Answers1

-1

Thank you all for your advices! I now used dput(Catrv_all), and here is the output of my data:

structure(list(treatment = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("Co", "CoP", "HDPE"), class = "factor"),
    expTime = c(0L, 3L, 6L, 24L, 96L, 168L, 336L, 672L, 0L, 3L, 
    6L, 24L, 96L, 168L, 336L, 672L, 0L, 3L, 6L, 24L, 96L, 168L, 
    336L, 672L), MP = c(0.056481655, 0.098508038, 0.097108112, 
    0.056848278, 0.082198187, 0.052261369, 0.022911461, 0.023901656, 
    0.056481655, 0.124866733, 0.125732967, 0.07986102, 0.071233133, 
    0.128376543, 0.331948, 0.121689155, 0.056481655, 0.186735799, 
    0.137477095, 0.41251914, 0.093364945, 0.085760245, 0.249371764, 
    0.187693319), Lip = c(9.848221569, 11.62875399, 9.530378924, 
    12.67745734, 14.14610784, 11.44140636, 11.55310567, 12.37321851, 
    9.848221569, 8.889567938, 12.5142123, 13.79770638, 11.26698845, 
    14.67064904, 14.56027915, 15.24772977, 9.848221569, 12.22424265, 
    13.05104725, 12.96830215, 12.10175574, 14.66505958, 13.67550035, 
    11.65168387), Cat = c(6.681571728, 7.321681629, 4.939885929, 
    7.73812502, 6.85066487, 9.317238053, 8.309505248, 9.33338377, 
    6.681571728, 7.517468479, 7.151607966, 9.074518192, 6.350614893, 
    9.749092742, 9.335634354, 11.43658695, 6.681571728, 6.164473371, 
    9.416062149, 9.19813927, 8.041328941, 8.736550013, 9.788258534, 
    10.55471537), CI = c(120.5252336, 110.1709456, 112.9077575, 
    110.9032308, 101.0274926, 101.1970679, 107.1464111, 97.42950278, 
    120.5252336, 101.7284063, 132.6162567, 108.7251954, 107.2199383, 
    102.9096767, 100.9637646, 101.6655302, 120.5252336, 102.1888777, 
    111.9139996, 113.7840225, 104.4767637, 103.1984161, 96.67797683, 
    95.59369834)), .Names = c("treatment", "expTime", "MP", "Lip", 
"Cat", "CI"), class = "data.frame", row.names = c(NA, -24L))

Hopefully it would help to reconstruct my code. Again to my question: yes, I would like to show exposure Time as well on one of the axes (if this is possible). And secondly, I want to show a kind of "time series" (from 0h to 672 h) and the behaviour of both MP and Lip for all treatment groups. So my first idea was: y-axes: MP, x-axes bottom: Lip, x-axes on top: exposure Time --> plot values for all treatment groups in the right order for exposure time (from 0 to 672). Try to plot a trend line. In fact, I want a visual evidence, that MP behavior (over time) led to changes in Lipofuscinaccumulation for different treatment groups.

@Jake Kaupp: I am not sure, how to facet_wrap in ggplot. May you specify that a bit please?

Apple
  • 1
  • 1