は、あなたが何を望むか、このですか?その質問を見ると、それぞれの標本について平均値size
とvmt
を取って、それらの値を使って線形回帰を実行したかったようです。それはdplyr
パッケージを使用して行われました。
library(ggplot2)
library(dplyr)
## df is same as VMT
df <- read.csv("uro7.csv")[,1:7]
df2 <- filter(df, sex == "F") %>%
group_by(specimen) %>%
select(-one_of(c("date", "time", "sex", "turn"))) %>%
summarize_all(mean)
Select
4つの列をドロップし、summarize_all
はsize
とvmt
の平均をとります。これにより列名が保持されるので、aes(x = size, y = vmt)
はggplot
に含めることができ、geom_boxplot
とgeom_smooth
の両方に渡すことができます。一方、aes(group = specimen, colour = sex, fill = sex)
は、df2
にこれらの列がないため、geom_boxplot
である必要があります。
ggplot(df,aes(x=size, y=vmt)) +
geom_boxplot(aes(group=specimen,colour=sex,fill=sex))+
scale_colour_brewer(palette="Set1")+
#geom_point()+
ylab("VTM (°C)")+
xlab("size") +
geom_smooth(data=df2,
col = "red", method = lm, se = FALSE)
![enter image description here](https://i.stack.imgur.com/veBLo.png)
また、元のコードに関するいくつかのコメント:
あなたがsex == "F"
とmethod = lm, se = FALSE
が(どちらが私のコードで修正されている)aes
に含めるべきではないはずです。