geom_segment() - connecting x axis

Hi,

could you please help we with this plot so that geom_segment() connect x axis correctly?

library(ggplot2)

median_age <- data.frame(           
country = c("Italie","Nemecko",
  "Portugalsko",
 "Recko","Bulharsko",
"Litva","Spanelsko",
 "Chorvatsko",
 "Slovinsko","Lotyssko",
 "Rakousko","Madarsko",
 "Finsko",
"Nizozemsko","Cesko",
"Rumunsko","Estonsko",
 "Dansko","Francie",
"Belgie","Polsko",
"Slovensko",
 "Svedsko",
 "Spojene kraloství","Malta",
 "Lucembursko","Irsko",
 "Kypr"),
value = c(46.7,46,45.2,44.9,
44.5,44.1,44,44,
 44,43.5,43.4,43,
42.9,42.7,42.6,
42.5,42.1,41.9,
41.8,41.7,41,40.6,
40.5,40.2,40,39.5,
37.7,37.7),
colour = c("black","black",
"black","black",
"black","black","black",
 "black","black",
"black","black",
"black","black",
"black","#CD3C33",
 "black","black",
"black","black","black",
"black","black",
"black","black",
 "black","black",
"black","black")
                                                                         )

age_plot <- ggplot(median_age,aes(x = reorder(country, -value),y=value)) 

age_plot +   geom_hline(yintercept=43.3, linetype="dashed", color = "grey50")+
  geom_point(stat="identity",color='#254061',shape=18, size=7,position = position_dodge(width=1)) +
  labs(title="Mediánový věkový průměr v zemích EU (2019)",x="",y="") +
  geom_segment(aes(xend=value),yend=0)+
  theme(axis.text.x=element_text(angle=45, hjust=1,color=median_age$colour,size=11),
        axis.text.y=element_text(size=11,color="black"),
        panel.background = element_blank(),
        plot.title = element_text(face="bold", size=15, vjust=1),
        axis.title.y = element_text(face="bold", size=13,vjust=2))+ 
  annotate(geom="text", x=25.5, y=44.5, label="průměr EU", color="grey50",size=4.5)+
  ylim(35,50)
#> Warning: Vectorized input to `element_text()` is not officially supported.
#> Results may be unexpected or may change in future versions of ggplot2.

image

geom_segment(aes(xend=country),yend=0)+
1 Like

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