ggplot使用geom_point绘制平均值

时间:2016-08-05 18:37:09

标签: r ggplot2 count mean

您好我想在点图上添加平均值作为点而不是总计数。我有两个分类因素:岸区和深度。还有一个计数数据列,标记为1或0,表示在岸区某个深度处存在粒子。这是一个设想情节的例子。

## load ggplot
library(ggplot2)
## my own code for interest
szone.df <- ddply(transect.df, .(shore.zone, depth.cm), summarise, count = sum(count))
summary(szone.df)

## reproducible data
shorezone <- c("HWM","HWM","HWM","HWM","IT","IT","IT","IT","ST","ST","ST","ST")
depth <- c("50","25", "10", "0","50","25","10", "0","50","25","10","0")
count <- c(0, 1, 9, 232, 0 ,27, 0, 15, 0, 1, 0, 8)

## create data frame
szone <- data.frame(shorezone, depth, count)
szone
## depth to factor
szone$depth <- as.factor(szone$depth)
## reverse factor 
szone$depth = with(szone, factor(depth, levels = rev(levels(depth))))

## plot data that I would like the grpah to look like but with mean values
s.zone <- ggplot(szone) + aes(x = shorezone, y = depth, size = count) +
geom_point() +
scale_size_area(name = "Count", max_size = 45) + 
theme(legend.title = element_text(colour = "black", size = 20, face = "bold"), 
    legend.text = element_text(colour = "black", size = 18),
    axis.title.x = element_text(colour = "black", size = 22, face = "bold", margin = margin(15,0,0,0)),
    axis.title.y = element_text(colour = "black", size = 22, face = "bold", margin = margin(0,15,0,0)),
    axis.text.x = element_text(size = "18"),
    axis.text.y = element_text(size = "18"), 
    panel.grid.minor = element_blank(),
    panel.grid.major = element_line(colour = "grey")) +
scale_fill_grey(start = 0.1, end = 0.6) +
scale_x_discrete(name = "Shore Zone")+
scale_y_discrete(name = "Depth (cm)")
s.zone

然而,按照上面的代码,我使用ddply预先提取并总结了计数数据。当使用我的原始数据帧时,ggplot将计数数据绘制为1或0.如果我添加size = count,则返回数据集中的0的频率(附加的照片)。这是代码之前没有求和的代码:

szone <- ggplot(transect.df, aes(x = shorezone, y = depth, size = count)) +
geom_count() +
scale_size_area() +
theme(legend.title = element_text(colour = "black", size = 20, face = "bold"), 
    legend.text = element_text(colour = "black", size = 18),
    axis.title.x = element_text(colour = "black", size = 22, face = "bold", margin = margin(15,0,0,0)),
    axis.title.y = element_text(colour = "black", size = 22, face = "bold", margin = margin(0,15,0,0)),
    axis.text.x = element_text(size = "18"),
    axis.text.y = element_text(size = "18"), 
    panel.grid.minor = element_blank(),
    panel.grid.major = element_line(colour = "grey")) + 
scale_fill_grey(start = 0.1, end = 0.6) +
scale_x_discrete(name = "Shore Zone") +
scale_y_discrete(name = "Depth (cm)")
szone

我已经读过ggplot可以处理绘图总和&amp;意味着没有围绕/聚合原始数据集。我花了一整天的时间在圈子里如此道歉如果不清楚,R已经对我造成伤害。

我希望在不预先计算计数数据的情况下绘制数据,这样我就可以使用ggplot函数生成并绘制平均值。计数数据是否定的。在岸区内某一深度发现的颗粒。 +我想找到不同深度的每个岸区的平均粒子数,并使用ggplot函数进行绘图。任何提示将非常感谢。谢谢。

plotted 50 values but data is 0 [count 1 & 0]

0 个答案:

没有答案