ggplot - grand median

Hi,

I have this plot

which I created using ggplot, factors and facets. Could you dvise me how to add a common median line for each facet?

Many thanks,

Jakub

( ggplot(plot8beta, aes(x=factor(EPG,levels=c("ENF","EFDD","Greens/EFA","GUE/NGL","ALDE","ECR","S&D","EPP")), y=value,colour=EPG)) +
geom_jitter(position=position_jitter(width=0.2,height=0),alpha=0.7)+
geom_boxplot(alpha=0,show.legend=FALSE,lwd=1)+
scale_color_manual(limits = c("EPP","S&D","ECR","ALDE","GUE/NGL","Greens/EFA","EFDD","ENF"),
values = c("#ffdb05","#fc1414","#106da8","#ec008c","#c6262c","#44911d","#00abbc","black")) +
guides(color=guide_legend(""))+
theme(legend.position="bottom",
axis.text.y=element_blank(),
axis.ticks=element_blank(),
text=element_text(size=13))+
labs(x="",y="",title="",subtitle=expression(paste("Figure 8: Ideal Position Estimated by IRT in the 8"^"th"*" European Parliament")))+

coord_flip()+
facet_grid(variable~.) )

Here's an example using the built-in iris data frame. This approach requires ggplot2 version 3.0. In the code below y="virginica" is a dummy grouping variable to prevent a separate vline for each level of Species. Any value of Species (that's present in the data) will work.

library(tidyverse)

# Set a seed for reproducibility
set.seed(2)
iris %>%
  # Add grouping variable for faceting
  mutate(group = sample(c("group1", "group2"), nrow(iris), replace=TRUE)) %>% 
  ggplot(aes(y=Species, x=Petal.Length)) +
    stat_summary(fun=median, geom="vline", aes(xintercept=stat(x), y="virginica"), colour="grey20") +
    geom_boxplot(aes(colour=Species)) +
    facet_grid(group ~ .) +
    guides(colour=FALSE) +
    theme_bw()

Created on 2020-05-25 by the reprex package (v0.3.0)

1 Like

Thank you very much! I checked the version ggplot2 package but still I got this error:

library(ggplot2)
plot8beta<-data.frame(
  stringsAsFactors = FALSE,
                    row.names = c("1","29",
                                  "57","85","113","141","169","197",
                                  "225","253","281","309","337","365","393",
                                  "421","449","477","505","533","561","589",
                                  "617","645","673","701","729","757",
                                  "785","813","841","869","897","925","953",
                                  "981","1009","1037","1065","1093","1121",
                                  "1149","1177","1205","1233","1261",
                                  "1289","1317","1345","1373","1401","1429"),
                          EPG = c("EFDD",
                                  "ALDE","EPP","EPP","S&D","EPP","EPP",
                                  "S&D","ALDE","ALDE","S&D","S&D","EPP","ENF",
                                  "S&D","S&D","ECR","EPP","Greens/EFA",
                                  "S&D","ECR","ALDE","S&D","GUE/NGL","EPP",
                                  "ALDE","Greens/EFA","EPP","S&D","EFDD",
                                  "EPP","EFDD","GUE/NGL","EPP","Greens/EFA",
                                  "ENF","Greens/EFA","EPP","S&D","ALDE","ALDE",
                                  "S&D","EPP","Greens/EFA","S&D","EPP",
                                  "ECR","GUE/NGL","EPP","GUE/NGL","S&D",
                                  "EFDD"),
                           EP = c("EP8",
                                  "EP8","EP8","EP8","EP8","EP8","EP8","EP8",
                                  "EP8","EP8","EP8","EP8","EP8","EP8",
                                  "EP8","EP8","EP8","EP8","EP8","EP8","EP8",
                                  "EP8","EP8","EP8","EP8","EP8","EP8",
                                  "EP8","EP8","EP8","EP8","EP8","EP8","EP8",
                                  "EP8","EP8","EP8","EP8","EP8","EP8","EP8",
                                  "EP8","EP8","EP8","EP8","EP8","EP8",
                                  "EP8","EP8","EP8","EP8","EP8"),
                        value = c(-0.379774242996712,0.172315972440176,
                                  0.508771375843188,0.735039922061224,-0.0293302188505968,
                                  0.488793320045722,0.308600110879289,
                                  -0.126657264650647,0.034159060407793,0.0978260034285467,
                                  -0.121273309161861,-0.138765859773851,
                                  0.636554976021266,-0.0512297661682134,
                                  -0.108291772901163,-0.137474882596259,
                                  0.237202866634456,0.0515686354827501,-0.633685137601414,
                                  -0.138543322004933,0.229710312045323,
                                  0.16976906020358,-0.124000426216816,-0.640680518772482,
                                  0.632176298730038,0.182133291153883,
                                  0.0597572759751731,0.300926182066494,
                                  0.594026312274795,-0.0768024002434432,0.213015201862035,
                                  -0.464690847421952,-0.130741159014226,
                                  -0.141132931728519,0.359063454395183,
                                  -0.484702695864316,0.472995183059961,0.249977354995377,
                                  0.530843230444484,0.150692540827256,
                                  0.188067474315055,0.476434445817727,0.218584627498155,
                                  0.369565589855805,0.460152467874211,
                                  0.411953625494457,-0.277238106259614,
                                  -0.220648817516735,0.247530849978582,0.0345924579021892,
                                  0.769167419206866,-0.544629742856372),
                     variable = as.factor(c("Dimension 1",
                                            "Dimension 1","Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2","Dimension 2"))
           )

ggplot(plot8beta, aes(x=factor(EPG,levels=c("ENF","EFDD","Greens/EFA","GUE/NGL","ALDE","ECR","S&D","EPP")), y=value,colour=EPG)) +
  geom_jitter(position=position_jitter(width=0.2,height=0),alpha=0.7)+
  geom_boxplot(alpha=0,show.legend=FALSE,lwd=1)+
  scale_color_manual(limits = c("EPP","S&D","ECR","ALDE","GUE/NGL","Greens/EFA","EFDD","ENF"),
                     values = c("#ffdb05","#fc1414","#106da8","#ec008c","#c6262c","#44911d","#00abbc","black")) +
  guides(color=guide_legend(""))+
  theme(legend.position="bottom", 
        axis.text.y=element_blank(),
        axis.ticks=element_blank(),
        text=element_text(size=13))+
  labs(x="",y="",title="",subtitle=expression(paste("Figure 8: Ideal Position Estimated by IRT in the 8"^"th"*" European Parliament")))+
  coord_flip()+
  facet_grid(variable~.)+
  stat_summary(fun=median, geom="vline", aes(xintercept=stat(x),y="EPP"), colour="grey20")
#> Error: Discrete value supplied to continuous scale

I think that was happening because your code has the x-axis as the discrete axis and uses coord_flip() to get it to appear as the y-axis. coord_flip() is no longer necessary in ggplot2 v3.0. You just assign the variables to the axis you want them on and ggplot has some logic to automatically orient the plot. I've updated your example to switch the x and y aesthetics and to remove coord_flip. I've also made a few other changes.

# Vector of color assignments
col.vec = c("#ffdb05","#fc1414","#106da8","#ec008c","#c6262c","#44911d","#00abbc","black") %>% 
  set_names(c("EPP","S&D","ECR","ALDE","GUE/NGL","Greens/EFA","EFDD","ENF"))

plot8beta %>%  
  # Turn EPG into factor before plot so that factor levels are same for y and colour aesthetics
  mutate(EPG = factor(EPG, levels=c("ENF","EFDD","Greens/EFA","GUE/NGL","ALDE","ECR","S&D","EPP"))) %>% 
  # Assign EPG to y-axis and value to x-axis; no need for coord_flip()
  ggplot(aes(y=EPG, x=value, colour=EPG)) +
    geom_jitter(position=position_jitter(width=0.2,height=0), alpha=0.7)+
    geom_boxplot(alpha=0, lwd=1) +
    stat_summary(fun=median, geom="vline", aes(xintercept=stat(x),y="EPP"), colour="grey20") +
    scale_color_manual(values=col.vec) +
    theme_bw(base_size=13) +
    labs(x="", y="", title="",
         subtitle=expression(paste("Figure 8: Ideal Position Estimated by IRT in the 8"^"th"*" European Parliament")))+
    facet_grid(variable ~ .)+
    guides(colour=FALSE)

1 Like

Hi,

I would like to add common median line for each facet.

library(ggplot2)
plot8beta<-data.frame(
  stringsAsFactors = FALSE,
                    row.names = c("1","29",
                                  "57","85","113","141","169","197",
                                  "225","253","281","309","337","365","393",
                                  "421","449","477","505","533","561","589",
                                  "617","645","673","701","729","757",
                                  "785","813","841","869","897","925","953",
                                  "981","1009","1037","1065","1093","1121",
                                  "1149","1177","1205","1233","1261",
                                  "1289","1317","1345","1373","1401","1429"),
                          EPG = c("EFDD",
                                  "ALDE","EPP","EPP","S&D","EPP","EPP",
                                  "S&D","ALDE","ALDE","S&D","S&D","EPP","ENF",
                                  "S&D","S&D","ECR","EPP","Greens/EFA",
                                  "S&D","ECR","ALDE","S&D","GUE/NGL","EPP",
                                  "ALDE","Greens/EFA","EPP","S&D","EFDD",
                                  "EPP","EFDD","GUE/NGL","EPP","Greens/EFA",
                                  "ENF","Greens/EFA","EPP","S&D","ALDE","ALDE",
                                  "S&D","EPP","Greens/EFA","S&D","EPP",
                                  "ECR","GUE/NGL","EPP","GUE/NGL","S&D",
                                  "EFDD"),
                           EP = c("EP8",
                                  "EP8","EP8","EP8","EP8","EP8","EP8","EP8",
                                  "EP8","EP8","EP8","EP8","EP8","EP8",
                                  "EP8","EP8","EP8","EP8","EP8","EP8","EP8",
                                  "EP8","EP8","EP8","EP8","EP8","EP8",
                                  "EP8","EP8","EP8","EP8","EP8","EP8","EP8",
                                  "EP8","EP8","EP8","EP8","EP8","EP8","EP8",
                                  "EP8","EP8","EP8","EP8","EP8","EP8",
                                  "EP8","EP8","EP8","EP8","EP8"),
                        value = c(-0.379774242996712,0.172315972440176,
                                  0.508771375843188,0.735039922061224,-0.0293302188505968,
                                  0.488793320045722,0.308600110879289,
                                  -0.126657264650647,0.034159060407793,0.0978260034285467,
                                  -0.121273309161861,-0.138765859773851,
                                  0.636554976021266,-0.0512297661682134,
                                  -0.108291772901163,-0.137474882596259,
                                  0.237202866634456,0.0515686354827501,-0.633685137601414,
                                  -0.138543322004933,0.229710312045323,
                                  0.16976906020358,-0.124000426216816,-0.640680518772482,
                                  0.632176298730038,0.182133291153883,
                                  0.0597572759751731,0.300926182066494,
                                  0.594026312274795,-0.0768024002434432,0.213015201862035,
                                  -0.464690847421952,-0.130741159014226,
                                  -0.141132931728519,0.359063454395183,
                                  -0.484702695864316,0.472995183059961,0.249977354995377,
                                  0.530843230444484,0.150692540827256,
                                  0.188067474315055,0.476434445817727,0.218584627498155,
                                  0.369565589855805,0.460152467874211,
                                  0.411953625494457,-0.277238106259614,
                                  -0.220648817516735,0.247530849978582,0.0345924579021892,
                                  0.769167419206866,-0.544629742856372),
                     variable = as.factor(c("Dimension 1",
                                            "Dimension 1","Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 1",
                                            "Dimension 1","Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2",
                                            "Dimension 2","Dimension 2","Dimension 2"))
           )

ggplot(plot8beta, aes(x=factor(EPG,levels=c("ENF","EFDD","Greens/EFA","GUE/NGL","ALDE","ECR","S&D","EPP")), y=value,colour=EPG)) +
  geom_jitter(position=position_jitter(width=0.2,height=0),alpha=0.7)+
  geom_boxplot(alpha=0,show.legend=FALSE,lwd=1)+
  scale_color_manual(limits = c("EPP","S&D","ECR","ALDE","GUE/NGL","Greens/EFA","EFDD","ENF"),
                     values = c("#ffdb05","#fc1414","#106da8","#ec008c","#c6262c","#44911d","#00abbc","black")) +
  guides(color=guide_legend(""))+
  theme(legend.position="bottom", 
        axis.text.y=element_blank(),
        axis.ticks=element_blank(),
        text=element_text(size=13))+
  labs(x="",y="",title="",subtitle=expression(paste("Figure 8: Ideal Position Estimated by IRT in the 8"^"th"*" European Parliament")))+
  coord_flip()+
  facet_grid(variable~.)

Do you have any advise?

Thank you!

Thank you very much! This is exactly what I needed. Btw I like how you changed the graph. Would you have any suggestion for this one?

library(ggplot2)
IDEA6 <- data.frame(
  stringsAsFactors = FALSE,
                               EPG = c("EPP","S&D","ALDE","GUE/NGL",
                                       "Greens/EFA","IND/DEM","UEN","EPP","S&D",
                                       "ALDE","GUE/NGL","Greens/EFA","IND/DEM",
                                       "UEN","EPP","S&D","ALDE","GUE/NGL",
                                       "Greens/EFA","IND/DEM","UEN","EPP",
                                       "S&D","ALDE","GUE/NGL","Greens/EFA",
                                       "IND/DEM","UEN"),
                             value = c(0.606663848576983,0.435449741423812,
                                       0.875982039500883,0.241187383645329,
                                       0.245063890554234,0.660252068762424,
                                       0.697990665370454,1.6991938702997,
                                       0.701411434736525,0.563727194358423,
                                       0.509125288218384,0.447870530198116,0.346223350759741,
                                       1.23968525283648,0.738957131300102,
                                       0.618149733830559,1.24675399954734,
                                       0.245740502962094,0.22913405610721,
                                       0.649950463069727,1.81819963630087,1.05841996831619,
                                       0.469758901162118,0.936790743843378,
                                       1.26673290825527,0.385335174157912,
                                       0.608452097747784,0.739426715836192),
                          variable = as.factor(c("lrgen",
                                                 "lrgen","lrgen","lrgen",
                                                 "lrgen","lrgen","lrgen","EU",
                                                 "EU","EU","EU","EU","EU","EU",
                                                 "lrecon","lrecon","lrecon",
                                                 "lrecon","lrecon","lrecon",
                                                 "lrecon","galtan","galtan",
                                                 "galtan","galtan","galtan",
                                                 "galtan","galtan"))
                )

ggplot(IDEA6, aes(y=factor(EPG,levels=c("IND/DEM","UEN","Greens/EFA","GUE/NGL","ALDE","S&D","EPP")), x=value,fill=EPG,width=0.6)) +
  geom_bar(stat = "identity")+
  scale_fill_manual(limits = c("EPP","S&D","ALDE","GUE/NGL","Greens/EFA","UEN","IND/DEM"),
                    values = c("#ffdb05","#fc1414","#ec008c","#c6262c","#44911d","#5568a0","#80bdf0")) +
  guides(fill=guide_legend(""))+
  theme(legend.position="bottom", 
        axis.text.y=element_blank(),
        axis.ticks=element_blank(),
        text=element_text(size=14))+
  labs(x="",y="",title=expression(paste("Figure 3: Ideological Diversity in the 6"^"th"*" European Parliament")))+
  facet_grid(variable~.,switch="both")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.