Home > Net >  Using ggplot to label my data using months instead of days
Using ggplot to label my data using months instead of days

Time:07-28

I wish to plot my data by using the date on the x axis and the number of project launched on the y axis I am currently using this code

plot1 <- ggplot() 
  geom_line(data=data0,mapping = aes(x = date, y = launches, group=1) )   
  geom_line(data=data0,mapping = aes(x = date, y = US, group=1), colour="blue" ) 
  ggtitle("Kickstarter")

However, i realised that i have so many dates that the Y axis becomes difficult to read Instead i would wish for the Y axis to just show the months

Is there any way to do this ?

structure(list(date = c("2021-01-01", "2021-01-02", "2021-01-03", 
"2021-01-04", "2021-01-05", "2021-01-06"), launches = c(4, 0, 
0, 0, 8, 4), pledged = c(50278.64, 0, 0, 0, 366279.590415302, 
172073.0471292), backers = c(2880, 0, 0, 0, 6588, 3528), total_goal = c(24000, 
0, 0, 0, 148000, 60000), mean_goal = c(6000, 0, 0, 0, 18500, 
15000), US = c(4, 0, 0, 0, 4, 0), `number of success` = c(4, 
0, 0, 0, 8, 4), duration_days = c(30, 0, 0, 0, 31, 30), Twitter = c(1324L, 
1548L, 1297L, 1585L, 1636L, 1583L), replies = c(882L, 1252L, 
910L, 1018L, 810L, 1000L), likes = c(22859L, 24375L, 17854L, 
20341L, 19521L, 19401L), retweets = c(8621L, 8239L, 6141L, 6728L, 
6938L, 6842L)), row.names = c(NA, 6L), class = "data.frame")

edit: my apologies, i inversed teh x and y axis in my explanation

CodePudding user response:

I changed your 'example' dataset to better illustrate a potential solution:

library(ggplot2)
data0 <- structure(list(date = c("2021-01-01", "2021-01-15", "2021-02-01", 
                        "2021-02-15", "2021-03-01", "2021-03-15"), launches = c(4, 0, 
                                                                                0, 0, 8, 4), pledged = c(50278.64, 0, 0, 0, 366279.590415302, 
                                                                                                         172073.0471292), backers = c(2880, 0, 0, 0, 6588, 3528), total_goal = c(24000, 
                                                                                                                                                                                 0, 0, 0, 148000, 60000), mean_goal = c(6000, 0, 0, 0, 18500, 
                                                                                                                                                                                                                        15000), US = c(4, 0, 0, 0, 4, 0), `number of success` = c(4, 
                                                                                                                                                                                                                                                                                  0, 0, 0, 8, 4), duration_days = c(30, 0, 0, 0, 31, 30), Twitter = c(1324L, 
                                                                                                                                                                                                                                                                                                                                                      1548L, 1297L, 1585L, 1636L, 1583L), replies = c(882L, 1252L, 
                                                                                                                                                                                                                                                                                                                                                                                                      910L, 1018L, 810L, 1000L), likes = c(22859L, 24375L, 17854L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                           20341L, 19521L, 19401L), retweets = c(8621L, 8239L, 6141L, 6728L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 6938L, 6842L)), row.names = c(NA, 6L), class = "data.frame")

plot1 <- ggplot(data0)  
  geom_line(aes(x = date, y = launches, group = 1) )   
  geom_line(aes(x = date, y = US, group = 1), colour="blue")  
  ggtitle("Kickstarter")
plot1


# Change the format from "character" to "date"
data0$date <- as.Date(data0$date)

# Then you can change the breaks on the x axis
plot2 <- ggplot(data0)  
  geom_line(aes(x = date, y = launches, group = 1) )   
  geom_line(aes(x = date, y = US, group = 1), colour="blue")  
  ggtitle("Kickstarter")  
  scale_x_date(date_breaks = "1 month")
plot2

Created on 2022-07-27 by the enter image description here

  •  Tags:  
  • r
  • Related