R 辅助y轴中的特定数据

R 辅助y轴中的特定数据,r,ggplot2,R,Ggplot2,这种语言对我来说还是有点陌生。我想制作一个复杂的双轴图形,数据按组绘制 我的数据的性质STAT。我将以代码形式编写,否则我无法发布帖子: 4个时间点(“0”、“3”、“5”和“7”),列日 数据分为5组,列SNu(“1”、“2”、“3”、“4”、“5”)或SNa(每组的实际名称) 每组和时间点有4个值,列代表图可以绘制这四个值的平均值 数据1基于一天和第二天实际测量值之间的面积,SAr列(有些值为0,介于0和205之间,有些值为小数)。我想在主y轴上绘制它 数据2,列DW(值介于0和1之间,均

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

我的数据的性质
STAT
。我将以代码形式编写,否则我无法发布帖子:

  • 4个时间点(“0”、“3”、“5”和“7”),列日
  • 数据分为5组,列SNu(“1”、“2”、“3”、“4”、“5”)或SNa(每组的实际名称)
  • 每组和时间点有4个值,列代表图可以绘制这四个值的平均值
  • 数据1基于一天和第二天实际测量值之间的面积,SAr列(有些值为0,介于0和205之间,有些值为小数)。我想在主y轴上绘制它
  • 数据2,列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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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
我尝试将我创建的数字因子放在开头,但是
Day
SAr
的长度不同

model.frame.default(公式=Day~SAr)中出错:
可变长度不同(适用于“SAr”)

我还尝试使用
密度
函数和
几何区域
来实现这一点,但它们都没有达到我想要的效果

2.绘图数据2 我希望数据二(
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
我知道在因子类型()的变量上不可能有一个连续的尺度。但他们的解决方案对我也不起作用

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`


如果有人能帮我解决这两个问题,我将不胜感激。由于我是这段代码的新手,我也鼓励你问我可能有关联的具体细节,我没有在帖子中添加。此外,我的代码中的任何改进(即使与我的问题无关)都是非常受欢迎的。

如果您
dput(STAT)
(您的数据)并发布结果,则应该会增加获得帮助的几率。如果你不能发布你的数据,可以很方便地发布一些与你类似的打印和粘贴假数据,以放入R中,重现你的问题。欢迎使用StackOverflow!请阅读有关的信息,特别是如何给出一个答案。这将使其他人更容易帮助你。
#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
Error: Discrete value supplied to continuous scale
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))