0

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 :)

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

ean_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),
  )

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
user1317221_G
  • 15,087
  • 3
  • 52
  • 78
  • 1
    I use and recommend https://r-graph-gallery.com as a great source for ideas, for both *"what kinds of plots do a good job depicting the data"* as well as *"how do I make it"*. The examples include pictures, sample data, and code to reproduce them, very informative. FYI, I don't know what (if any) substantive changes were made, but https://posit.co/resources/cheatsheets/?type=posit-cheatsheets&_page=2/ has a newer version of the ggplot2 cheatsheet, updated in 2021. – r2evans May 22 '23 at 11:47
  • With a reproducible example it will be easier to be more specific in the any answers. See here for help on how to do this https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – user1317221_G May 22 '23 at 11:50

2 Answers2

1

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.

user1317221_G
  • 15,087
  • 3
  • 52
  • 78
0

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 <- 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()

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.

Nico
  • 463
  • 5
  • 11