如何在R Studio中创建这样的图表?(箱线图,点在柱状图上方)

huangapple go评论69阅读模式
英文:

How to create such diagram in r studio? (boxplot, points over bar plot)

问题

请查看这个图表

嗨,

首先:我对R和编程都很新手。我想用自己的代码复制一份数据分析。我想在R Studio中使用ggplot2来以类似的方式表示我的数据(请看图片)。我已经在一个单独的数据集中提取了均值。

这可能是什么样的可视化?我已经尝试查看ggplot2速查表,但没有弄明白。

谢谢你的帮助 如何在R Studio中创建这样的图表?(箱线图,点在柱状图上方)

我正在尝试从数据集中提取均值数据,像这样:

mean_values_polVertrauen <- polVertrauen %>%
  summarize(
    BUNDESVERFASSUNGSGERICHT = mean(pt02),
    BILDUNG = mean(pt11),
    POLIZEI = mean(pt14),
    JUSTIZ = mean(pt08),
    GESUNDHEITSWESEN = mean(pt01),
    VERWALTUNG = mean(pt04),
    BUNDESTAG = mean(pt03),
    BUNDESREGIERUNG = mean(pt12),
    EUROPKOMMISSION = mean(pt19),
    EUROPPARLAMENT = mean(pt20),
    PARTEIEN = mean(pt15)
  )

我的输出为:

mean_values_polVertrauen

是这样的:

variable    value
1  BUNDESVERFASSUNGSGERICHT 4.249473
2                   BILDUNG 4.200120
3                   POLIZEI 3.950045
4                    JUSTIZ 3.579898
5          GESUNDHEITSWESEN 3.941920
6                VERWALTUNG 3.486909
7                 BUNDESTAG 3.058381
8           BUNDESREGIERUNG 3.055673
9           EUROPKOMMISSION 2.512489
10           EUROPPARLAMENT 2.553416
11                 PARTEIEN 2.190190
英文:

Please have a look at this plot

Hi,

first up: I'am pretty new to R and programming in general. I want to reproduce a analysis of data with my own code. I would like to represent my data with ggplot 2 in r studio in a similar way (please take a look at the picture). I have already extracted to mean values in a seperate data set.

What kind of visualisation could that be? I have tried looking at the ggplot2 cheatsheet but could not figure it out.

Thanks for your help 如何在R Studio中创建这样的图表?(箱线图,点在柱状图上方)

I'am trying to extract the mean data from the set like this:

ean_values_polVertrauen &lt;- polVertrauen %&gt;%
  summarize(
    BUNDESVERFASSUNGSGERICHT = mean(pt02),
    BILDUNG = mean(pt11),
    POLIZEI = mean(pt14),
    JUSTIZ = mean(pt08),
    GESUNDHEITSWESEN = mean(pt01),
    VERWALTUNG = mean(pt04),
    BUNDESTAG = mean(pt03),
    BUNDESREGIERUNG = mean(pt12),
    EUROPKOMMISSION = mean(pt19),
    EUROPPARLAMENT = mean(pt20),
    PARTEIEN = mean(pt15),
  )

my output for

mean_values_polVertrauen

is

variable    value
1  BUNDESVERFASSUNGSGERICHT 4.249473
2                   BILDUNG 4.200120
3                   POLIZEI 3.950045
4                    JUSTIZ 3.579898
5          GESUNDHEITSWESEN 3.941920
6                VERWALTUNG 3.486909
7                 BUNDESTAG 3.058381
8           BUNDESREGIERUNG 3.055673
9           EUROPKOMMISSION 2.512489
10           EUROPPARLAMENT 2.553416
11                 PARTEIEN 2.190190

答案1

得分: 1

你展示的图形看起来像是geom_point的组合,可能还包括geom_errorbargeom_linerange,例如:

ggplot(data, aes(x = x, y = y)) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.0)) +
  geom_point() +
  coord_flip()

有多种方法可以实现你展示的图形。如果有可重现的示例,我们可以提供更具体的帮助。

英文:

The plot you show looks like a combination of geom_point and perhaps geom_errorbar or geom_linerangee.g.

ggplot(data, aes(x = x, y = y)) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.0)) +
  geom_point() +
  coord_flip()

There are multiple ways to achieve the plot you show. With a reproducible example, we can help be more specific.

答案2

得分: 0

首先,请在下次提问时提供一个可重现的例子(例如使用 dput)。

现在来看你的绘图代码。以下代码重现了图形。有一些例外情况:点的形状要么是自定义的,要么是geom_errorbargeom_point的组合。由于你没有提供误差条的数据,所以下面的代码只绘制了点。
图的主题是自定义的。你可以选择用例如 这个(RPubs) 页面构建自己的主题,或者从 这里(r-charts) 选择现有的主题。

df <- data.frame(variable = c("BUNDESVERFASSUNGSGERICHT",
                              "BILDUNG",
                              "POLIZEI",
                              "JUSTIZ",
                              "GESUNDHEITSWESEN",
                              "VERWALTUNG",
                              "BUNDESTAG",
                              "BUNDESREGIERUNG",
                              "EUROPKOMMISSION",
                              "EUROPPARLAMENT",
                              "PARTEIEN"),
                 value = c(4.249473,
                           4.200120,
                           3.950045,
                           3.579898,
                           3.941920,
                           3.486909,
                           3.058381,
                           3.055673,
                           2.512489,
                           2.553416,
                           2.190190))
library(ggplot2)
ggplot(data = df, aes(x = reorder(variable, value), y = value, label = round(value, 1))) +
  geom_point() +
  geom_text(hjust=-.45, vjust=.4) +
  labs(y = "Vertrauen", x = "") +
  coord_flip() +
  theme_minimal()

这个图很简单:初始化你的数据,设置x和y的值,以及你的标签值(在这种情况下是values),添加一个geom_pointgeom_text图层并翻转坐标。不要忘记对数据进行reorder以获得“从高到低”的表示。添加x轴标签和你想要的主题 —— 完成。

英文:

Firstly, please put a repdoducible example in your question next time (with dput for example).

Now to your plot. Following code reproduces the graphic. There are a few exceptions: The shape of the points is either custom or a combination of geom_errorbar and geom_point. Since you did not provide data for an error bar, the code below just produces points.
The theme of the plot is custom. You can either build your theme with for example this (RPubs) page or select a existing theme from here (r-charts).

df &lt;- data.frame(variable = c(&quot;BUNDESVERFASSUNGSGERICHT&quot;,
                              &quot;BILDUNG&quot;,
                              &quot;POLIZEI&quot;,
                              &quot;JUSTIZ&quot;,
                              &quot;GESUNDHEITSWESEN&quot;,
                              &quot;VERWALTUNG&quot;,
                              &quot;BUNDESTAG&quot;,
                              &quot;BUNDESREGIERUNG&quot;,
                              &quot;EUROPKOMMISSION&quot;,
                              &quot;EUROPPARLAMENT&quot;,
                              &quot;PARTEIEN&quot;),
                 value = c(4.249473,
                           4.200120,
                           3.950045,
                           3.579898,
                           3.941920,
                           3.486909,
                           3.058381,
                           3.055673,
                           2.512489,
                           2.553416,
                           2.190190))
library(ggplot2)
ggplot(data = df, aes(x = reorder(variable, value), y = value, label = round(value, 1))) +
  geom_point() +
  geom_text(hjust=-.45, vjust=.4) +
  labs(y = &quot;Vertrauen&quot;, x = &quot;&quot;) +
  coord_flip() +
  theme_minimal()

The plot is simple: Initialize your data, set x and y values as well as the values for your label (in this case values), add a geom_point and geom_text layer and flip the coordinates. Don't forget to reorder your data to get the "from high to low" representation. Add the x-axis label and your desired theme --> done.

huangapple
  • 本文由 发表于 2023年5月22日 19:34:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定