我正在尝试在ggplot中创建一个图表,根据不同的性别,治疗方法,时间段和季节显示动物的平均家庭范围大小。我在R中说错误
错误:美学必须是长度1或与数据(24)相同:x,y,颜色,形状“
我已阅读有关此错误的类似帖子,但我还未能弄明白。这些列中没有NA,我的数值变量也被这样处理。不确定错误是否与子数据集的需要有关,但我不明白我应该怎么做。我的代码运行正常,直到ggplot部分,它是以下内容:
library("ggplot2")
library("dplyr")
lion_HR_size <- read.csv(file = "https://dl.dropboxusercontent.com/u/23723553/lion_sample_data.csv",
header= TRUE, row.names=1)
# Mean of home range size by season, treatment, sex and time
Mean_HR <- lion_HR_size %>%
group_by(season, treatment, sex, time) %>%
summarize(
mean_HR = mean(Area_HR_km),
se_HR = sd(Area_HR_km)/sqrt(n()),
lwrHR = mean_HR - se_HR,
uprHR = mean_HR + se_HR)
limitsHR <- aes(ymin = lwrHR, ymax= uprHR)
ggplot(Mean_HR,
aes(x=season,
y= Mean_HR,
colour=season,
shape= season)) +
geom_point( size = 6, alpha = 0.5)+
facet_grid(sex ~ treatment+time)+
geom_errorbar(limitsHR, width = 0.1, col = 'red', alpha = 0.8)+
theme_bw()
根据要求,dput(Mean_HR)输出如下:
dput(Mean_HR) 结构(列表(季节=结构)(c(1L,1L,1L,1L,1L,1L,1L, 1L,2L,2L,2L,2L,2L,2L,2L,2L,3L,3L,3L,3L,3L,3L,3L, 3L),. Label = c(“Early_dry”,“Late_dry”,“Wet”),class =“factor”), 处理=结构(c(1L,1L,1L,1L,2L,2L,2L,2L,1L, 1L,1L,1L,2L,2L,2L,2L,1L,1L,1L,1L,2L,2L,2L,2L ),。Label = c(“C”,“E”),class =“factor”),性别=结构(c(1L, 1L,2L,2L,1L,1L,2L,2L,1L,1L,2L,2L,1L,1L,2L,2L, 1L,1L,2L,2L,1L,1L,2L,2L),. Label = c(“F”,“M”),class =“factor”), 时间=结构(c(1L,2L,1L,2L,1L,2L,1L,2L,1L,2L, 1L,2L,1L,2L,1L,2L,1L,2L,1L,2L,1L,2L,1L,2L),. Label = c(“A”, “B”),class =“factor”),mean_HR = c(141.594090181,138.327188493, 509.287443507692,345.296845642381,157.634028930833,188.202160663125, 252.464096340667,255.078012825,59.8485325981818,143.158189516522, 439.990400912593,175.410885601333,221.338774452381,100.942251723636, 127.961533612727,167.199563142143,120.60363022375,142.351764574211, 249.03854219,330.018734301176,123.992902995714,219.886321226667, 307.869373359167,296.019550844286),se_HR = c(18.6245437612391, 29.2548378154774,127.987824704623,78.9236194797204,208.8897993194466, 43.1314245224751,57.6327505533691,32.1129054260719,9.383853530199, 38.7678333459788,130.348285186224,31.707304307485,29.1561478797825, 15.4038723326613,18.1932127432015,37.791782522185,32.7089231722616, 33.2629181623941,46.1500408067739,88.8736578370159,15.8046627788777, 36.9665360444972,70.1560303348504,87.1340476758794),lwrHR = c(122.969546419761, 109.072350677523,381.29961880307,266.373226162661,136.744229611387, 141.07073614065,194.831345787298,222.965107398928,50.4646790679828, 104.390356170543,309.642115726369,143.703581293848,192.182626572598, 85.5383793909751,109.768320869526,129.407780619958,87.8947070514884, 109.088846411816,202.888501383226,241.145076464161,108.188240216837, 182.91978518217,237.713343024316,208.885503168406),uprHR = c(160.218633942239, 167.582026308477,637.275268212315,424.220465122101,178.52382825028, 227.3335851856,310.096846894036,287.190918251072,69.2323861283808, 181.9260228625,570.338686098816,207.118189908818,250.494922332163, 116.346124056298,146.154746355929,204.991345664328,153.312553396012, 175.614682736605,295.188582996774,418.892392138192,139.797565774592, 256.852857271164,378.025403694017,383.153598520165)),class = c(“grouped_df”, “tbl_df”,“tbl”,“data.frame”),row.names = c(NA,-24L),vars = list( 季节,治疗,性别),drop = TRUE,.Names = c(“季节”, “治疗”,“性别”,“时间”,“mean_HR”,“se_HR”,“lwrHR”,“uprHR” ))
有人可以帮我理解这个错误以及如何在我的代码中修复它吗?非常感谢!
答案 0 :(得分:1)
不完全确定自己为什么/ limitsHR <- ...
语句如何工作。我原本以为它无法在工作区中找到lwrHR
和uprHR
对象。
无论如何,ggplot有一个很好的函数mean_se()可以帮助你。
ggplot(data = lion_HR_size, mapping = aes(x = season, y = Area_HR_km,
colour=season, shape= season)) +
stat_summary(fun.data = mean_se) +
facet_grid(sex ~ treatment+time)+
theme_bw()