Niko
Niko

Reputation: 31

Why is the themes function not applying changes to the ggplot?

I want to change the text size of my y axis descrption and center my plottitle.

Everything coded within the themes function is not being applied to my graph.

Where is the problem?

finalchart = ggplot(euall,
                    aes(day, cumulative_cases_of_14_days_per_100000 ,
                        group = countriesAndTerritories)) +
  geom_bar(stat = "identity" ,
           col = "white" ,
           fill = "darkred") +
  facet_wrap("countriesAndTerritories") +
  geom_line(aes(y = rollmean(cumulative_cases_of_14_days_per_100000, 1, 
                             na.pad = TRUE), 
                col = "pink"), 
            show.legend = FALSE) +
  labs(title = "COVID infections in the European Union in September 2020" ,
       x = "\nSeptember" ,
       y = "Infections per 100'000\n" ,
       caption = "source: https://opendata.ecdc.europa.eu/covid19/casedistribution/csv") +
  theme(axis.text.y = element_text(size = 5) ,
        axis.title.y = element_text(size = 10) ,
        plot.title = element_text(hjust = 0.5)) +
  theme_minimal()

finalchart

Upvotes: 3

Views: 3031

Answers (1)

AndreasM
AndreasM

Reputation: 952

The problem is the order in which you call theme() and theme_minimal(). By calling theme_minimal() second your manual settings in theme() are overwritten.

library(ggplot2)
library(patchwork)

p1 <- ggplot(data = cars, aes(x = speed, y = dist)) +
  geom_point() +
  ggtitle("theme_minimal second") +
  theme(title = element_text(size = 24, color = "red", face = "bold")) +
  theme_minimal()

p2 <- ggplot(data = cars, aes(x = speed, y = dist)) +
  geom_point() +
  ggtitle("theme_minimal first") +
  theme_minimal() +
  theme(title = element_text(size = 24, color = "red", face = "bold"))

p1+p2

enter image description here

Upvotes: 3

Related Questions