Home > other >  I'm having problems with my ggplot2 theme system
I'm having problems with my ggplot2 theme system

Time:10-09

I'm having problems with my ggplot2 drawing, I don't know why, I've restarted Rstudio and its theme system can't be restored to the original, which is the default theme

library(tidyverse)
chic <- read_csv("./chicago-nmmaps-custom.csv")
ggplot(chic, aes(x = date, y = temp))   
  geom_point()

Here's the code I ran

enter image description here

This is what I got when I ran it

Normal should look like this, as shown below

enter image description here

CodePudding user response:

You could use theme_set to replace older themes like this:

library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt))  
  geom_point()
p

old <- theme_set(theme_bw())
p

theme_set(old)
p

Created on 2022-10-08 with reprex v2.0.2

CodePudding user response:

The problem is that column date is not a date object, it's a column of class "character". Coerce to class "Date" and the default grey theme is used.

str(chic)
#> 'data.frame':    5114 obs. of  9 variables:
#>  $ city    : chr  "chic" "chic" "chic" "chic" ...
#>  $ date    : chr  "1987-01-01" "1987-01-02" "1987-01-03" "1987-01-04" ...
#>  $ death   : int  130 150 101 135 126 130 129 109 125 153 ...
#>  $ temp    : num  31.5 33 33 29 32 40 34.5 29 26.5 32.5 ...
#>  $ dewpoint: num  31.5 29.9 27.4 28.6 28.9 ...
#>  $ pm10    : num  27.8 NA 33.7 40.8 NA ...
#>  $ o3      : num  4.03 4.58 3.4 3.94 4.4 ...
#>  $ time    : int  1 2 3 4 5 6 7 8 9 10 ...
#>  $ season  : chr  "winter" "winter" "winter" "winter" ...



#x11(width = 20, height = 5)

library(ggplot2)

chic |>
  dplyr::mutate(date = as.Date(date)) |>
  ggplot(aes(date, temp))  
  geom_point()  
  scale_x_date(date_breaks = "1 year", date_labels = "%Y")

Created on 2022-10-08 with reprex v2.0.2

  • Related