3

I want to plot my coefficients from three different models to show the shrinkage effect of the estimated coefficients with a dot-chart.

For more context: I am working with a hierarchical linear model and want to compare those estimates with the estimates of the complete pooling and the no pooling estimates.

Let's say we have a dataframe like this:

    a <- c(1,2,3,4)
    b <- c(2.5, 2.5, 2.5, 2.5)
    c <- c(1.2,2.3,2.8,3.7)
    city <- c("London", "Madrid", "Sidney", "Paris")
    df <- as.data.frame(cbind(city,a,b,c))
df <- df[order(df$a),]

I want to show them in a descending order kinda like in this picture but without the standard deviations, just the points. Is there a way to do that simply with ggplot?

RazorLazor
  • 71
  • 5

1 Answers1

2

Starting with your dataframe df, you can reshape your data using pivot_longer and conserve your data order by calling df$city in the scale_x_discrete:

library(tidyr)
df2 = df %>% pivot_longer(.,-city, names_to =  "Model", values_to = "value")

library(ggplot2)
ggplot(df2, aes(x = city, y = value, color = Model)) + geom_point() + coord_flip() + 
  scale_x_discrete(limits = df$city)

I re-arrange your dataframe df in order to get numerical values (using cbind in combination with dataframe tends to converts data into factor levels):

a <- c(1,2,3,4)
b <- c(2.5, 2.5, 2.5, 2.5)
c <- c(1.2,2.3,2.8,3.7)
city <- c("London", "Madrid", "Sidney", "Paris")
df <- data.frame(city,a,b,c)
df <- df[order(df$a),]

And you get the following graph enter image description here

dc37
  • 15,840
  • 4
  • 15
  • 32
  • Is it also possible to plot them in a overlapping manner without seperating the scales? (the purpose is to show that the points of model c are closer together than the points of model a which is harder to see in this plot than in a plot it a single scale) – RazorLazor Nov 25 '19 at 01:35
  • @RazorLazor, I updated my answer, basically the different scale was because the dataframe created with your code convert numerical values into factor levels, I modify your dataframe to get numerical values instead. But you will have to check this on your actual dataframe before plotting them. – dc37 Nov 25 '19 at 01:41
  • Looks neat! I will check it in the morning (very late now in Europe) and report my results. The dataset is a bit larger (85 groups), so I'd have to see if it still looks fine visually. – RazorLazor Nov 25 '19 at 01:44
  • Didn't work for the first try but it was my mistake. Works now, thanks! – RazorLazor Nov 25 '19 at 10:37