0

This language is still a bit alien to me. I want to make a complicate graph with two axis and data plotted by groups.

The nature of my data STAT. I will write it as code, otherwise I cannot manage to publish the post:

  • 4 time points ("0", "3", "5" and "7"), column Day.
  • Data divided in 5 groups, column SNu ("1", "2", "3", "4", "5") or SNa (the actual name of each group).
  • There are 4 values per group and time point, column Rep. Graph could plot the mean of these four values.
  • Data1 based on the area between the actual measures of one day and the following day, column SAr (some values are 0, between 0 and 205, some of them with decimals). I want to plot this in the primary y axis.
  • Data2, column DW (values between 0 and 1, all of them with 4 decimals). I want to plot this in the secondary axis.

I show below some modified data as an example.

structure(list(Sname = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("H4.8", "S302", "S309", 
"S313", "T.m"), class = "factor"), Snumber = c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), Day = c(0L, 3L, 
5L, 7L, 0L, 3L, 5L, 7L, 0L, 3L, 5L, 7L, 0L, 3L, 5L, 7L, 0L, 3L, 
5L, 7L, 0L, 3L, 5L, 7L, 0L, 3L, 5L, 7L, 0L, 3L, 5L, 7L, 0L, 3L, 
5L, 7L, 0L, 3L, 5L, 7L, 0L, 3L, 5L, 7L, 0L, 3L, 5L, 7L, 0L, 3L, 
5L, 7L, 0L, 3L, 5L, 0L, 3L, 5L, 7L, 0L, 3L, 5L, 7L, 0L, 3L, 5L, 
7L, 0L, 3L, 5L, 7L, 0L, 3L, 5L, 7L, 0L, 3L, 5L, 7L), Replica = c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), Diff = c(0L, 
0L, 160L, 200L, 0L, 10L, 140L, 160L, 0L, 0L, 50L, 170L, 0L, 10L, 
70L, 150L, 0L, 10L, 210L, 140L, 0L, 0L, 0L, 120L, 0L, 30L, 70L, 
160L, 0L, 20L, 110L, 140L, 0L, 30L, 190L, 150L, 0L, 10L, 80L, 
130L, 0L, 10L, 90L, 140L, 0L, 0L, 170L, 170L, 0L, 80L, 200L, 
410L, 0L, 10L, 150L, 0L, 90L, 200L, 390L, 0L, 50L, 220L, 600L, 
0L, 0L, 0L, 100L, 0L, 0L, 0L, 70L, 0L, 20L, 10L, 150L, 0L, 20L, 
40L, 140L), Sum = c(0L, 0L, 160L, 360L, 0L, 10L, 150L, 310L, 
0L, 0L, 50L, 220L, 0L, 10L, 80L, 230L, 0L, 10L, 220L, 360L, 0L, 
0L, 0L, 120L, 0L, 30L, 100L, 260L, 0L, 20L, 130L, 270L, 0L, 30L, 
220L, 370L, 0L, 10L, 90L, 220L, 0L, 10L, 100L, 240L, 0L, 0L, 
170L, 340L, 0L, 80L, 280L, 690L, 0L, 10L, 160L, 0L, 90L, 290L, 
680L, 0L, 50L, 270L, 870L, 0L, 0L, 0L, 100L, 0L, 0L, 0L, 70L, 
0L, 20L, 30L, 180L, 0L, 20L, 60L, 200L), Sumarea = structure(c(1L, 
1L, 17L, 33L, 1L, 2L, 16L, 29L, 1L, 1L, 3L, 22L, 1L, 2L, 9L, 
22L, 1L, 2L, 22L, 32L, 1L, 1L, 1L, 14L, 1L, 20L, 12L, 23L, 1L, 
13L, 15L, 24L, 1L, 20L, 22L, 31L, 1L, 2L, 11L, 21L, 1L, 2L, 12L, 
23L, 1L, 1L, 18L, 31L, 1L, 4L, 27L, 7L, 1L, 2L, 17L, 1L, 6L, 
28L, 5L, 1L, 30L, 25L, 10L, 1L, 1L, 1L, 12L, 1L, 1L, 1L, 8L, 
1L, 13L, 26L, 17L, 1L, 13L, 6L, 19L), .Label = c("0", "1,6", 
"12,5", "13,3", "147,5", "15", "152,5", "17,5", "20", "205", 
"22,5", "25", "3,3", "30", "32,5", "37,5", "40", "42,5", "45", 
"5", "52,5", "55", "57,5", "62,5", "67,5", "7,5", "70", "72,5", 
"75", "8,3", "85", "87,5", "90"), class = "factor"), Sumarea10 = c(0L, 
0L, 400L, 900L, 0L, 16L, 375L, 750L, 0L, 0L, 125L, 550L, 0L, 
16L, 200L, 550L, 0L, 16L, 550L, 875L, 0L, 0L, 0L, 300L, 0L, 50L, 
250L, 575L, 0L, 33L, 325L, 625L, 0L, 50L, 550L, 850L, 0L, 16L, 
225L, 525L, 0L, 16L, 250L, 575L, 0L, 0L, 425L, 850L, 0L, 133L, 
700L, 1525L, 0L, 16L, 400L, 0L, 150L, 725L, 1475L, 0L, 83L, 675L, 
2050L, 0L, 0L, 0L, 250L, 0L, 0L, 0L, 175L, 0L, 33L, 75L, 400L, 
0L, 33L, 150L, 450L), Dweight = structure(c(1L, 6L, 34L, 38L, 
1L, 7L, 32L, 45L, 1L, 8L, 31L, 48L, 1L, 9L, 30L, 44L, 1L, 11L, 
37L, 50L, 1L, 11L, 33L, 49L, 1L, 13L, 35L, 51L, 1L, 18L, 36L, 
52L, 1L, 21L, 47L, 53L, 1L, 19L, 43L, 54L, 1L, 20L, 46L, 56L, 
1L, 22L, 42L, 55L, 1L, 17L, 28L, 39L, 1L, 15L, 27L, 1L, 13L, 
26L, 41L, 1L, 17L, 29L, 40L, 1L, 5L, 10L, 24L, 1L, 3L, 14L, 24L, 
1L, 4L, 16L, 23L, 1L, 2L, 12L, 25L), .Label = c("0", "0,0003", 
"0,0006", "0,0007", "0,0008", "0,0011", "0,0017", "0,0026", "0,0033", 
"0,004", "0,0045", "0,0048", "0,005", "0,0051", "0,0053", "0,0055", 
"0,0056", "0,006", "0,007", "0,0074", "0,0082", "0,0086", "0,0142", 
"0,0204", "0,0222", "0,0333", "0,0342", "0,0345", "0,038", "0,0423", 
"0,0426", "0,0637", "0,0668", "0,0679", "0,0736", "0,0808", "0,0922", 
"0,0952", "0,0986", "0,0989", "0,0996", "0,1078", "0,1215", "0,1242", 
"0,1349", "0,1483", "0,1512", "0,1576", "0,1682", "0,1731", "0,1949", 
"0,2099", "0,262", "0,2676", "0,2742", "0,2808"), class = "factor"), 
    Wweight = structure(c(1L, 3L, 40L, 42L, 1L, 4L, 37L, 44L, 
    1L, 8L, 26L, 48L, 1L, 9L, 24L, 43L, 1L, 10L, 41L, 49L, 1L, 
    11L, 39L, 46L, 1L, 12L, 35L, 50L, 1L, 14L, 38L, 53L, 1L, 
    22L, 52L, 57L, 1L, 20L, 47L, 58L, 1L, 17L, 51L, 60L, 1L, 
    21L, 45L, 59L, 1L, 15L, 34L, 54L, 1L, 19L, 32L, 1L, 16L, 
    31L, 56L, 1L, 18L, 36L, 55L, 1L, 7L, 13L, 27L, 1L, 6L, 29L, 
    25L, 1L, 5L, 30L, 23L, 1L, 2L, 33L, 28L), .Label = c("0", 
    "0,0089", "0,0105", "0,0136", "0,0144", "0,0147", "0,0152", 
    "0,0201", "0,0265", "0,0339", "0,0345", "0,0371", "0,045", 
    "0,0463", "0,0569", "0,0583", "0,0587", "0,0596", "0,0602", 
    "0,0649", "0,069", "0,0834", "0,1264", "0,1829", "0,1897", 
    "0,1909", "0,1974", "0,2309", "0,3", "0,344", "0,3491", "0,3547", 
    "0,364", "0,3729", "0,3756", "0,3932", "0,4357", "0,4361", 
    "0,451", "0,4634", "0,479", "0,5109", "0,6594", "0,7182", 
    "0,7423", "0,7865", "0,7938", "0,8406", "0,8407", "0,9152", 
    "0,9347", "0,9675", "1", "1,0908", "1,1366", "1,1465", "1,6905", 
    "1,7799", "1,8875", "1,9493"), class = "factor")), class = "data.frame", row.names = c(NA, -79L))

    #Pretreat dataframe by creating factors for every column.
    STAT<- read.table("Biomass.txt", header=TRUE, fill=TRUE)
    SNa <- as.factor(STAT$Sname)
    SNu <- as.factor(STAT$Snumber)
    Day <- as.numeric(STAT$Day)
    Rep <- as.numeric(STAT$Replica)
    Dif <- as.numeric(STAT$Diff)
    Sum <- as.numeric(STAT$Sum)
    SAr10 <- as.numeric(STAT$Sumarea10)
    SAr <- c(SAr10/10)
    DW <- as.numeric(STAT$Dweight)
    WW <- as.numeric(STAT$Wweight)

    #I first tried to plot Dataone (`SAr`) as follows:

    points1 <- geom_point(aes(colour = SNa), size =.8)
    lines1 <- geom_smooth(method = loess, aes(colour = SNa), size =.5, se=TRUE, alpha=.2)
    text1 <- labs(title=expression (Biomass~and~CO[2]~production~summed~ area), x=expression(Time~" "~(days)), y=expression(CO[2]~production~sum~" "~(ppm)))
    g <- ggplot(data=STAT, aes(x=Day, y=SAr, group=SNa, fill=SNa, colour=SNa), par(mar=Marg))
    g <- g + points1 + lines1 + text1

This is the result:

grouped points and smooth lines

So far so good, but here start the problems.

1. SHADE

I would like to shade the area below the graphs. I have tried:

area1 <- geom_ribbon(data = STAT[STAT$Snumber == '1',],
aes(ymin = 0, ymax = predict(loess(Day ~ Sumarea))),
alpha = 0.3, fill = "#114477")

g <- g + points1 + lines1 + text1 + area1

plot(g) returns:

Error in loess(Day ~ Sumarea) : predictors must all be numeric

I have tried to put the numeric factors I created at the beginning, but Day and SAr do not have the same length

Error in model.frame.default(formula = Day ~ SAr) :
variable lengths differ (found for 'SAr').

I have also tried to make this with a density function and a geom_area but none of them resulted in what I wanted.

2. PLOT DATA2

I want the Datatwo (DW) attachted to the secondary y axis.

#Secondary y axis
y2 <- scale_y_continuous(sec.axis = sec_axis(~./150, name = "Dry 
weight"))
#Grouped bars per time point
bars2 <-  geom_bar(aes(factor(Day), DW), stat="identity", position = "dodge")

g <- g + points1 + lines1 + text1 + y2 + bars2

plot(g) returns:

Error: Discrete value supplied to continuous scale

I know that there cannot be a continuous scale on variable of the factor type (Plotting with ggplot2: "Error: Discrete value supplied to continuous scale" on categorical y-axis). But their solution does not work for me either.

ggplot(STAT[STAT$SNu == 1,], aes(x = STAT$Day, y = STAT$DW)) +
 scale_x_continuous(limits=c(0,7)) + 
 scale_y_continuous(limits=c(0,1))

Returning

Error: Aesthetics must be either length 1 or the same as the data (79): x, y`

If anyone can help me with this two issues it would be super appreciated. As I am new in this code, I also encourage you to ask me about specific details that might have relevance and I did not add in the post. Also any improvement in my code even not related with my questions would be very welcome.

  • 2
    If you `dput(STAT)` (your data) and post the result it should increase the odds to be helped. If you cannot publish your data, it's convenient to post some print&paste fake data similar to yours to put in R, to reproduce your problems. – s__ Aug 06 '18 at 13:35
  • 2
    Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and especially how to give a [_minimal_ & _reproducible_ example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Axeman Aug 06 '18 at 13:51

0 Answers0