在facet_wrap中绘制平均线

2024-03-17

我有以下数据集:

structure(list(Geschaeft = c(0.0961028525512254, 0.0753516756309475, 
0, 0.0722803347280335, 0, 0.000877706260971328), Gaststaette = c(0.0981116914423463, 
0.0789718659495242, 0.0336538461538462, 0.0905857740585774, 0, 
0.00175541252194266), Bank = c(0.100843712334271, 0.0717832023169218, 
0.00480769230769231, 0.025, 0.00571428571428572, 0.00965476887068461
), Hausarzt = c(0.0633989554037766, 0.0589573851882499, 0.0288461538461538, 
0.0217573221757322, 0.00685714285714286, 0.0128730251609128), 
    Einr..F..Aeltere = c(0.0337484933708317, 0.0550268928423666, 
    0.00480769230769231, 0, 0.00114285714285714, 0.000292568753657109
    ), Park = c(0.0738449176376055, 0.0726623913942904, 0.0625, 
    0.0846234309623431, 0.00228571428571429, 0.112053832650673
    ), Sportstaette = c(0.0449979911611089, 0.0612846503930492, 
    0.00480769230769231, 0.0619246861924686, 0.00114285714285714, 
    0), OEPNV = c(0.10847730012053, 0.089056681836988, 0.264423076923077, 
    0.135669456066946, 0, 0.185488589818607), Mangel.an.Gruenflaechen = c(0.0867818400964243, 
    0.071369466280513, 0.144230769230769, 0.117259414225941, 
    0.260571428571429, 0.186951433586893), Kriminalitaet = c(0.108316593009241, 
    0.083678113363674, 0.389423076923077, 0.139330543933054, 
    0.334857142857143, 0.216500877706261), Auslaender = c(0.00715146645239052, 
    0.0212039718659495, 0.0480769230769231, 0.0550209205020921, 
    0.0114285714285714, 0), Umweltbelastung = c(0.108879067898755, 
    0.0846607364501448, 0, 0.143828451882845, 0.376, 0.228203627852545
    ), Einr..f..Kinder = c(0.0693451185214946, 0.0825403392635499, 
    0.0144230769230769, 0.0527196652719665, 0, 0.0444704505558806
    ), Einr..f..Jugendliche = c(0, 0.0934526272238312, 0, 0, 
    0, 0.000877706260971328), count = c(1466, 1821, 81, 1491, 
    330, 793), cluster = c(1, 2, 3, 4, 5, 6)), .Names = c("Geschaeft", 
"Gaststaette", "Bank", "Hausarzt", "Einr..F..Aeltere", "Park", 
"Sportstaette", "OEPNV", "Mangel.an.Gruenflaechen", "Kriminalitaet", 
"Auslaender", "Umweltbelastung", "Einr..f..Kinder", "Einr..f..Jugendliche", 
"count", "cluster"), row.names = c(NA, -6L), class = "data.frame")

我用它排序

mdf <- melt(nbhpp[,-15], id.vars = 'cluster')
mdf <- transform(mdf, variable = reorder(variable, value, mean), y = cluster)

并绘制

ggplot(mdf, aes(x=variable, y=value, group=cluster, colour=factor(cluster))) + 
  geom_line() + 
  scale_y_continuous('Anteile', formatter = "percent") +
  scale_colour_hue(name='Cluster') +
  xlab('Infrastrukturmerkmal') +
  theme_bw() +
  opts(axis.text.x = theme_text(angle=90, hjust=1), legend.position = "none") +
  facet_wrap(~cluster, ncol=3)

如果我理解正确的话,变换函数按平均值对数据进行排序。但如何才能将这些平均值作为灰线包含到每个图中呢?

感谢您的帮助


UPDATE:

只是为了澄清:

如果我看一下重新排序语句的输出

with(mdf, reorder(variable, value, mean))

比我得到以下属性:

attr(,"scores")
   Einr..f..Jugendliche        Einr..F..Aeltere              Auslaender            Sportstaette 
             0.01572172              0.01583642              0.02381364              0.02902631 
               Hausarzt                    Bank               Geschaeft         Einr..f..Kinder 
             0.03211500              0.03630061              0.04076876              0.04391644 
            Gaststaette                    Park                   OEPNV Mangel.an.Gruenflaechen 
             0.05051310              0.06799505              0.13051918              0.14452739 
        Umweltbelastung           Kriminalitaet 
             0.15692865              0.21201772 

它们在图中从左(最低)到右(最高)排序。 问题是,如何用这些属性画一条线......


编辑后的答案

要添加一条包含聚类平均值的线,您需要构造一个data.frame包含数据。您可以从中提取值mdf:

meanscores <- attributes(mdf$variable)$scores
meandf <- data.frame(
  variable = rep(names(meanscores), 6),
  value    = rep(unname(meanscores), 6),
  cluster  = rep(1:6, each=14)
  )

然后使用绘图geom_line:

ggplot(mdf, aes(x=variable, y=value, group=cluster, colour=factor(cluster))) + 
  geom_line() + 
  scale_y_continuous('Anteile', formatter = "percent") +
  scale_colour_hue(name='Cluster') +
  xlab('Infrastrukturmerkmal') +
  theme_bw() +
  opts(axis.text.x = theme_text(angle=90, hjust=1), legend.position = "none") +
  facet_wrap(~cluster, ncol=3) +
  geom_line(data=meandf, aes(x=variable, y=value), colour="grey50")

原答案

我最初的解释是你想要一条具有整体平均值的水平线。

只需添加一个geom_hline图层到你的图上,并映射yintercept to mean(value):

ggplot(mdf, aes(x=variable, y=value, group=cluster, colour=factor(cluster))) + 
  geom_line() + 
  scale_y_continuous('Anteile', formatter = "percent") +
  scale_colour_hue(name='Cluster') +
  xlab('Infrastrukturmerkmal') +
  theme_bw() +
  opts(axis.text.x = theme_text(angle=90, hjust=1), legend.position = "none") +
  facet_wrap(~cluster, ncol=3) +
  geom_hline(aes(yintercept=mean(value)), colour="grey50")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在facet_wrap中绘制平均线 的相关文章

  • 将 r 数据框中的列字符串转换为数字

    我有一个数据框 其中有一列字符串 如下所示 mydata lt c 1 356670 35 355030 1 356670 35 355030 1 356620 35 355890 1 356930 35 358660 1 357000 3
  • 带有 geom_errorbar 的position_dodge

    我有以下代码 require ggplot2 pd lt position dodge 0 3 ggplot dt aes x Time y OR colour Group geom errorbar aes ymin CI lower y
  • 如何在 switch 语句中将向量作为参数传递

    我对问题的谷歌搜索没有返回有用的结果和文档 switch没有告诉我如何做 所以我希望我能在这里得到答案 假设我有一个向量 cases lt c one two three 我想使用 switch 语句并将这些元素作为 switch 语句的参
  • mclapply 调用应该嵌套吗?

    正在筑巢parallel mclapply是个好主意吗 require parallel ans lt mclapply 1 3 function x mclapply 1 3 function y y x unlist ans Outpu
  • 使用 gtable 排列 ggplot 绘图(具有相同宽度的 grobs)以创建 2x2 布局

    我正在尝试使用 grobs 和 gtable 将 4 个 ggplot2 图排列成 2x2 网格 我不知道如何设置宽度 也不知道如何设置非 1xn 或 nx1 排列 使用此代码 data iris a lt ggplot iris aes
  • 使用 X11 窗口的 R 脚本仅打开一秒钟

    我正在通过 Linux Mint 16 命令行运行 R 脚本 它包含我想在窗口中显示的箱线图 所以我使用 x11 函数来创建该窗口 这是我的代码 testdata lt data frame sample 1 1000 size 100 r
  • R中的不定积分

    我正在计算方程的不定积分 我将加速度计的数据通过可视化 C 程序输入到 R 中 然后就可以很简单地得出一个方程来表示加速度曲线 这一切都很好 但是我还需要计算撞击速度 根据我在高中时代的理解 我的加速度曲线的不定积分将产生速度方程 我知道执
  • 了解用于处理色边距的scale_fill_continuous_divergingx参数输入

    这个问题是我上一个问题的延续here https stackoverflow com questions 58718527 setting midpoint for continuous diverging color scale on a
  • ggplot堆叠条 - 隐藏标签但保留标签位置

    我在 ggplot 中有一个堆积条形图 其中 geom text 标签位于每个条形的中心 我想隐藏小条上的标签 以便图表看起来不会过于拥挤 我可以使用下面的代码来完成此操作 但它会弄乱标签的位置 正如您在下面的链接图片中看到的那样 它们不再
  • 按列分组的数据帧上 R 中的行之间的差异

    我希望通过 app name 获得不同版本的计数差异 我的数据集如下所示 app name version id count difference 这是数据集 data structure list app name structure c
  • 修复 ggplot 中构面中的数据顺序

    我在使用 ggplot 绘制数据时遇到问题 我无法使每个方面内的数据正确排序 我的样本数据是 data lt structure list Parameter c 0 1 0 7 0 0 0 2 0 2 0 7 0 0 0 1 0 3 0
  • R 中的 as.numeric 有什么问题? [复制]

    这个问题在这里已经有答案了 gt X864291X8X74 1 8 0000000000 9 0000000000 10 0000000000 6 0000000000 8 0000000000 10 Levels 0 0000000000
  • 在嵌套 tibbles 上应用 ntile

    我正在尝试申请ntile在一些嵌套的小标题上 但我似乎无法让它工作 你能看出我错在哪里吗 data iris iris gt group by Species gt mutate quintile ntile Petal Length 5
  • 根据不平凡的标准有效合并两个数据帧

    正在接听这个问题 https stackoverflow com questions 18821862 data selection error 18823432 18823432昨晚 我花了一个小时试图找到一个没有增长的解决方案data
  • 删除 R 中具有重复属性的行

    我有一个大数据框 其中包含以下列 ID time OS IP 该数据帧的每一行对应一个条目 在该数据框中对于某些IDs存在多个条目 行 我想删除这些多行 显然 同一 ID 的其他属性会有所不同 或者换句话说 我只想要每个 ID 一个条目 行
  • 循环中的knitr模板和子文档

    圣诞节前我之前问过跨多个 knitr 文档的单一样式表 https stackoverflow com questions 20370584 single style sheet across multiple knitr document
  • 确定向量中是否存在元素的最有效方法

    我有几种算法取决于确定元素是否存在于向量中的效率 在我看来 这 in 这相当于is element 应该是最有效的 因为它只返回一个布尔值 在测试了几种方法之后 令我惊讶的是 这些方法是迄今为止效率最低的 以下是我的分析 随着向量大小的增加
  • 按具有作业的组划分的 R 分位数

    我有以下 df group rep seq 1 3 30 variable runif 90 5 0 7 5 df data frame group variable 我需要 i 按组定义分位数 ii 将每个人分配到相对于其组的分位数 因此
  • 抑制 R 中的错​​误消息

    我正在 R 中运行模拟研究 有时 我的模拟研究会产生错误消息 当我在函数中实现模拟研究时 当出现此错误消息时模拟停止 我知道抑制错误是不好的做法 但此时对我来说 除了抑制错误然后继续下一个模拟 直到达到我喜欢运行的模拟总数为止 没有其他选择
  • 在 R Shiny 中,如何使用可排序 js 将其在列表中出现的顺序次数附加到每个列表元素?

    下面的可重现代码适用于将元素从一个面板拖动到另一个面板 并在 拖动到 面板中自动使用 HTML CSS 对拖入的每个元素进行排名顺序编号 但是 我现在尝试附加到每个 拖动到 列表元素的末尾 使用某种形式的paste0 我假设 该元素在 拖至

随机推荐