1

I am playing around with a dataset and practicing out some visualizations using ggplot2. My dataset has a column year with 'int' datatype. While plotting, the years get converted to decimal forms. I do not want that.

Following is the code:

cases_over_11_years = Suicides_in_India %>% 
                         select(Year, Total) %>% 
                         arrange(Year) %>% 
                         group_by(Year) %>% 
                         summarize(Total =  sum(Total))

options(repr.plot.width = 12, repr.plot.height = 10)# plot dimensions


plot1 = ggplot(data = cases_over_11_years)
plot1 + geom_step(aes(x = Year, y = Total), stat = "identity", size = 1, color = "red") +
        labs( title = "Change in numbers over the time for 11 years",
              x = "Year",y = "Cases per year")+
        theme(axis.text = element_text(size = 18)) +
        theme(axis.title = element_text(size = 20)) +
        theme(plot.title = element_text(size=22))

This produces the plot:

enter image description here

I want x-axis labels simply in YYYY format. Also, I don't want to change the data type to the character.

Attaching sample dataset

structure(list(State = c("Punjab", "Manipur", "Haryana", "Karnataka", 
"Punjab", "Puducherry", "Arunachal Pradesh", "Nagaland", "Chandigarh", 
"Tamil Nadu"), Year = c(2001L, 2002L, 2008L, 2002L, 2004L, 2002L, 
2001L, 2009L, 2005L, 2009L), Type_code = c("Causes", "Means_adopted", 
"Causes", "Means_adopted", "Causes", "Causes", "Means_adopted", 
"Means_adopted", "Means_adopted", "Causes"), Type = c("Cancellation/Non-Settlement of Marriage", 
"By Drowning", "Dowry Dispute", "By Consuming Insecticides", 
"Illness (Aids/STD)", "Cancer", "By Machine", "By Drowning", 
"By Hanging", "Insanity/Mental Illness"), Gender = c("Male", 
"Male", "Female", "Female", "Male", "Female", "Female", "Male", 
"Male", "Female"), Age_group = c("45-59", "60+", "0-14", "45-59", 
"0-14", "60+", "0-14", "45-59", "0-14", "15-29"), Total = c(0L, 
0L, 0L, 62L, 0L, 0L, 0L, 0L, 0L, 38L)), class = "data.frame", row.names = c(NA, 
-10L))

Thankyou for your time :)

r2evans
  • 141,215
  • 6
  • 77
  • 149
classy_BLINK
  • 161
  • 8

1 Answers1

0

You can control where the ticks are placed by specifying scale_x_continuous(breaks=...). For example, we can use base R's axisTicks to generate a reasonable set of numbers:

plot1 = ggplot(data = cases_over_11_years)
plot1 + geom_step(aes(x = Year, y = Total), stat = "identity", size = 1, color = "red") +
        labs( title = "Change in numbers over the time for 11 years",
              x = "Year",y = "Cases per year")+
        theme(axis.text = element_text(size = 18)) +
        theme(axis.title = element_text(size = 20)) +
        theme(plot.title = element_text(size=22)) +
  scale_x_continuous(breaks = ~ axisTicks(., log = FALSE))

enter image description here

If this doesn't work perfectly with your real data, then you can always update the function to account for integers-only (perhaps hastily unique(round(axisTicks(., log = FALSE), 0))).

r2evans
  • 141,215
  • 6
  • 77
  • 149