辅助y轴上的特定数据

时间:2018-08-06 13:21:54

标签: r ggplot2

这种语言对我来说还是有点陌生​​。我想制作一个复杂的图形,其中有两个轴,并且按组绘制了数据。

我的数据STAT的性质。我将其编写为代码,否则我将无法发布该帖子:

  • 4个时间点(“ 0”,“ 3”,“ 5”和“ 7”)在“日期”列中。
  • 数据分为5组,分别为SNu列(“ 1”,“ 2”,“ 3”,“ 4”,“ 5”)或SNa(每组的实际名称)。
  • 每个组和每个时间点有4个值,列Rep。Graph可以绘制这四个值的平均值。
  • 数据1基于一天和第二天的实际度量之间的区域SAr列(一些值为0,介于0和205之间,其中一些带有小数)。我想在主要的y轴上绘制它。
  • Data2,DW列(值介于0和1之间,所有值均带有4个小数)。我想在辅助轴上绘制它。

下面我以一些修改后的数据为例。

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, 4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, 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

这是结果:

grouped points and smooth lines

到目前为止,一切都很好,但是从这里开始出现问题。

1。阴影

我想对图表下方的区域进行阴影处理。我尝试过:

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)返回:

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

我尝试将我创建的数字因子放在开头,但是DaySAr的长度不同

  

model.frame.default(formula = Day〜SAr)中的错误:
  可变长度有所不同(找到“ SAr”)。

我还尝试使用density函数和geom_area来实现这一目标,但是没有一个实现了我想要的结果。

2。绘图DATA2

我希望将Datatwo(DW)附加到次要y轴。

#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)返回:

Error: Discrete value supplied to continuous scale

我知道在因子类型(Plotting with ggplot2: "Error: Discrete value supplied to continuous scale" on categorical y-axis)的变量上不能有连续标度。但是他们的解决方案也不适合我。

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))

返回

  

错误:美学必须为长度1或与数据相同   (79):x,y`

如果有人可以帮助我解决这两个问题,将不胜感激。由于我是这段代码的新手,因此我也鼓励您向我询问可能相关的特定细节,因此我没有在帖子中添加。同样,即使我的代码中与我的问题无关的任何改进也将受到欢迎。

0 个答案:

没有答案