本文介绍了用线条连接方块图(ggplot2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个boxplot显示多个框。我想将每个盒子的平均值与一条线连接起来。 boxplot默认情况下不显示平均值,而中间线只显示中值。我试过了 $ pre $ g $ p $ ggplot(data,aes(x = xData,y = yData,group = g)) + geom_boxplot() + stat_summary(fun.y = mean,geom =line) 这是行不通的。 有趣的是,在做 stat_summary (fun.y = mean,geom =point) 绘制每个框中的中间点。为什么line不起作用? 类似这样但是使用ggplot2, http://www.aliquote.org/articles/tech/RMB/c4_sols/plot45.png library(ggplot2)$ (1000)b $ bx< - 因子(rep(1:10,100))y df ggplot(df,aes(x = x,y = y))+ geom_boxplot()+ stat_summary(fun.y = mean,geom =line ,aes(group = 1))+ stat_summary(fun.y = mean,geom =point) 更新: 设置group = 1的一些说明:我认为我在Hadley Wickham的书中找到了解释 ggplot2:用于数据分析的优雅图形。他在第51页写道: 不同层次的团体。我们想根据汇总的不同级别绘制汇总。不同的图层可能具有不同的群体美学,因此有些显示个人等级数据,而另一些则显示较大群组的摘要。 基于前面的例子,假设我们想要添加一条平滑的线到刚创建的图上,基于 on所有男孩的年龄和身高。如果我们对使用相同的分组,那么我们可以使用该分支的平滑性获得图4.4中的第一幅图。 p + geom_smooth(aes(group = Subject), method =lm,se = F) 这不是我们想要的;我们有无意中为每个男孩添加了一条平滑线。这个新图层需要不同的组审美观,group = 1,,这样新的一行将基于的所有数据,如第二个图所示数字。修改后的图层看起来像这样: p + geom_smooth(aes(group = 1), method =lm,size = 2 ,se = F) [...]在平滑图层中使用aes(group = 1)适合单行 best适合所有男孩。 I have a boxplot showing multiple boxes. I want to connect the mean for each box together with a line. The boxplot does not display the mean by default, instead the middle line only indicates the median. I tried ggplot(data, aes(x=xData, y=yData, group=g)) + geom_boxplot() + stat_summary(fun.y=mean, geom="line")This does not work.Interestingly enough, doing stat_summary(fun.y=mean, geom="point")draws the median point in each box. Why would "line" not work?Something like this but using ggplot2, http://www.aliquote.org/articles/tech/RMB/c4_sols/plot45.png 解决方案 Is that what you are looking for?library(ggplot2)x <- factor(rep(1:10, 100))y <- rnorm(1000)df <- data.frame(x=x, y=y)ggplot(df, aes(x=x, y=y)) +geom_boxplot() +stat_summary(fun.y=mean, geom="line", aes(group=1)) +stat_summary(fun.y=mean, geom="point")Update:Some clarification about setting group=1: I think that I found an explanation in Hadley Wickham's book "ggplot2: Elegant Graphics for Data Analysis". On page 51 he writes: Different groups on different layers. Sometimes we want to plot summaries based on different levels of aggregation. Different layers might have different group aesthetics, so that some display individual level data while others display summaries of larger groups. Building on the previous example, suppose we want to add a single smooth line to the plot just created, based on the ages and heights of all the boys. If we use the same grouping for the smooth that we used for the line, we get the first plot in Figure 4.4. p + geom_smooth(aes(group = Subject), method="lm", se = F) This is not what we wanted; we have inadvertently added a smoothed line for each boy. This new layer needs a different group aesthetic, group = 1, so that the new line will be based on all the data, as shown in the second plot in the figure. The modified layer looks like this: p + geom_smooth(aes(group = 1), method="lm", size = 2, se = F) [...] Using aes(group = 1) in the smooth layer fits a single line of best fit across all boys." 这篇关于用线条连接方块图(ggplot2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!