Home > Enterprise >  Problem with plot in R: reducing spce within labels and bars, but x axis ticks disappear
Problem with plot in R: reducing spce within labels and bars, but x axis ticks disappear

Time:07-22

I have the following code with its corresponding plot:

ggplot(df2, aes(x=Fecha.inicio, xend=Fecha.final, y=Ministro.a, yend=Ministro.a, color=Presidente))  
  theme_minimal()  #use ggplot theme with black gridlines and white background
  geom_segment(size=3)   #increase line width of segments in the chart
  theme(plot.title=element_text(size=10, face="bold", hjust=0.5)) 
  theme(legend.title = element_text(size=9)) 
  theme(legend.background = element_rect(fill = "white", linetype="solid", colour="darkblue", size=0.05)) 
  theme(legend.text=element_text(size = 9, colour = "black")) 
  theme(legend.key.height= unit(0.1, 'cm'),
        legend.key.width= unit(0.3, 'cm'),
        legend.key.size= unit(0.2, 'cm')) 
  theme(axis.title.y = element_blank(),
        axis.text.y = element_text(size=8),
        axis.text.x = element_text(size=8)) 
  theme(axis.ticks.length = unit(0, "mm")) 
  theme(legend.position = c(0.8, 0.8)) 
  #scale_x_continuous(expand = c(.01, .01))  
  scale_fill_identity(guide = "none")  
  scale_color_viridis(discrete=T,
                      breaks=c("Toledo", "García", "Humala", "De PPk a Sagasti","Castillo")) 
  labs("title"="Perú. Duración de los ministros de Agricultura (2001-2022)", x="") 
  labs(caption="  Elaboración: Ivan Ramírez (@peonpasado_)\n          Fecha: 19/07/2022") 
  theme(plot.caption= element_text(size=7,
                                   color="black",
                                   face="bold",
                                   hjust = -0.28,
                                   vjust = 1))

enter image description here

I wanted to reduce the space within the bars and the label in the y axis. So, I added the theme that it is commented in the previous code. This is the result.

enter image description here

As you see, the values of the x axis have changed and they moved one grid to the left. I want them to be the same as they were in the first graph, since it shows how long the agriculture ministers have lasted in Peru in the last 20 years. I tried with this in the scale_x_continuos line:

scale_x_continuous(expand = c(.01, .01), breaks=c(2000, 2005, 2010, 2015, 2020, 2025))

As a consequence, the all the x axis values disappeared. They also disappeared when I tried with this:

scale_x_continuous(expand = c(.01, .01), breaks=seq(2000,2025,5000))

Hope you can help me to solve the problem.

Additional info

They suggested me to show some of the data I used for the plot. This is the output of

dput(head(df2, 20))

Note that in the plot I'm not using the variables "Sexo", "Fecha.nacimiento", "Días.cargo", "year", "month" and "day".

structure(list(Fecha.inicio = structure(c(19149, 19134, 19031, 
19024, 18837, 18584, 18578, 18172, 17966, 17623, 17540, 17010, 
16125, 15544, 15319, 15183, 15107, 14866, 14436, 14166), class = "Date"), 
    Sector = c("Agricultura", "Agricultura", "Agricultura", "Agricultura", 
    "Agricultura", "Agricultura", "Agricultura", "Agricultura", 
    "Agricultura", "Agricultura", "Agricultura", "Agricultura", 
    "Agricultura", "Agricultura", "Agricultura", "Agricultura", 
    "Agricultura", "Agricultura", "Agricultura", "Agricultura"
    ), Ministro.a = structure(1:20, .Label = c("A. Alencastre", 
    "J. Arce", "O. Zea", "A. Ramos", "V. Mayta", "F. Tenorio", 
    "F. Hurtado", "J. L. Montenegro", "F. Muñoz", "G. Mostajo", 
    "J. Arista", "J. M. Hernández", "J. M. Benites", "M. von Hesse", 
    "L. Ginocchio", "M. Caillaux", "J. Villasante", "R. Quevedo", 
    "A. de Córdoba", "C. Leyton", "I. Benavides", "J. J. Salazar", 
    "M. Manrique", "Á. Quijandría/2", "J. León", "E. Gonzáles", 
    "Á. Quijandría/1"), class = "factor"), Fecha.nacimiento = c("", 
    "31/07/1970", "13/08/1973", "", "", "10/03/1957", "", "23/10/1965", 
    "27/11/1971", "18/08/1973", "18/08/1959", "27/08/1948", "21/06/1967", 
    "10/09/1964", "25/04/1954", "23/09/1950", "7/01/1962", "", 
    "27/09/1950", "8/01/1952"), Sexo = c("Hombre", "Hombre", 
    "Hombre", "Hombre", "Hombre", "Hombre", "Hombre", "Hombre", 
    "Mujer", "Hombre", "Hombre", "Hombre", "Hombre", "Hombre", 
    "Hombre", "Hombre", "Hombre", "Hombre", "Hombre", "Hombre"
    ), Presidente = c("Castillo", "Castillo", "Castillo", "Castillo", 
    "Castillo", "De PPk a Sagasti", "De PPk a Sagasti", "De PPk a Sagasti", 
    "De PPk a Sagasti", "De PPk a Sagasti", "De PPk a Sagasti", 
    "De PPk a Sagasti", "Humala", "Humala", "Humala", "Humala", 
    "García", "García", "García", "García"), year = c(2022, 2022, 
    2022, 2022, 2021, 2020, 2020, 2019, 2019, 2018, 2018, 2016, 
    2014, 2012, 2011, 2011, 2011, 2010, 2009, 2008), month = c(6, 
    5, 2, 2, 7, 11, 11, 10, 3, 4, 1, 7, 2, 7, 12, 7, 5, 9, 7, 
    10), day = c(6L, 22L, 8L, 1L, 29L, 18L, 12L, 3L, 11L, 2L, 
    9L, 28L, 24L, 23L, 11L, 28L, 13L, 14L, 11L, 14L), Fecha.final = structure(c(19194, 
    19149, 19134, 19031, 19024, 18837, 18584, 18578, 18172, 17966, 
    17623, 17540, 17010, 16125, 15544, 15319, 15183, 15107, 14866, 
    14436), class = "Date"), Días.Cargo = c(45, 15, 103, 7, 187, 
    253, 6, 406, 206, 343, 83, 530, 885, 581, 225, 136, 76, 241, 
    430, 270), numero = 1:20), row.names = c(3L, 6L, 15L, 24L, 
49L, 72L, 91L, 135L, 149L, 173L, 192L, 221L, 253L, 272L, 280L, 
297L, 308L, 318L, 331L, 344L), class = "data.frame")

CodePudding user response:

Here is a way.
Since an object of class "Date" is represented in the x axis, use an appropriate date scale to customize it. With scale_x_date date breaks and labels can be set using an intuitive syntax.

I have also created a custom theme, simplifying the code of the plot itself.

library(ggplot2)
library(viridis)
#> Loading required package: viridisLite

theme_ministros_de_Agricultura <- function(){ 
  theme_minimal() % replace%  #
    theme(
      plot.title = element_text(size = 10, face="bold", hjust = 0.5),
      plot.caption= element_text(size = 7, color="black", face = "bold",
                                 hjust = -0.28, vjust = 1),
      legend.title = element_text(size = 9),
      legend.position = c(0.8, 0.8),
      legend.background = element_rect(colour = "darkblue", fill = "white", 
                                       linetype = "solid", size = 0.05),
      legend.text = element_text(size = 9, colour = "black"),
      legend.key.height = unit(0.1, 'cm'),
      legend.key.width = unit(0.3, 'cm'),
      legend.key.size = unit(0.2, 'cm'),
      axis.title.y = element_blank(),
      axis.text.y = element_text(size = 8),
      axis.text.x = element_text(size = 8),
      axis.ticks.length = unit(0, "mm")
    )
}

ggplot(df2, aes(x = Fecha.inicio, xend = Fecha.final, y = Ministro.a, yend = Ministro.a, color = Presidente))  
  geom_segment(size=3)   # increase line width of segments in the chart
  # x axis is a date class
  scale_x_date(
    expand = c(.01, .01), 
    date_breaks = "2 years", 
    date_labels = "%Y"
  )  
  scale_fill_identity(guide = "none")  
  scale_color_viridis(
    discrete = TRUE,
    breaks = c("Toledo", "García", "Humala", "De PPk a Sagasti","Castillo")
  ) 
  labs(
    x="",
    title = "Perú. Duración de los ministros de Agricultura (2001-2022)",
    caption = "  Elaboración: Ivan Ramírez (@peonpasado_)\n          Fecha: 19/07/2022"
  )  
  theme_ministros_de_Agricultura()

Created on 2022-07-21 by the reprex package (v2.0.1)

  • Related