ggplot question

Can someone please tell me how to display two plots, derived from the same data, on the same grid? Thanks!

library(tidyverse)

(mydata <- tibble( 
  retweets = c(1:10,10:1),
  dates = rep(seq.Date(from=Sys.Date()-10,by="day",length.out = 10),2),
  category = c(rep("first",10),rep("second",10))
))

ggplot(data=mydata,
      mapping = aes(x=dates,y=retweets,colour=category)) + 
  geom_line() + 
  facet_wrap(~category,nrow = 2)

Thanks - this is what I already have, two plots side-by-side. What I would like to do is to put the two plots on the same grid.

Just do not facet the plot?

ggplot(data=mydata,
      mapping = aes(x=dates,y=retweets,colour=category)) + 
  geom_line()

Cac you supply us with some sample data? It may be that you need to reformat the data from wide format to long.

what do you mean, on the same grid? do you mean you want them overlayed i.e. on the same axis
as jrkrideau pointed out, if you dont want them facet wrapped, remove the facet wrap

I currently have this, which gives two separate plots, but I would like to overlay the results:
X <- my_data %>% subset(grepl("AAA", Tweet, ignore.case = TRUE))
XX <- ggplot(X, aes(x = Date, y = Retweets)) +
geom_line(color = "blue")
Y <- my_data %>% subset(grepl("BBB", Tweet, ignore.case = TRUE))
YY <- ggplot(Y, aes(x = Date, y = Retweets)) +
geom_line(color = "red")

I think what you want is this. Two source data, but same X and Y axis.

X <- my_data %>% subset(grepl("AAA", Tweet, ignore.case = TRUE))
Y <- my_data %>% subset(grepl("BBB", Tweet, ignore.case = TRUE))

ggplot() +
geom_line( X, aes(x = Date, y = Retweets), color = 'blue') +
geom_line( Y, aes(x = Date, y = Retweets), color = 'red')

1 Like

I am slow, but I finally got it, thank you!

1 Like

If your issue is resolved, please mark the most appropriate answer as the Solution so others can find it easily in the future. Cheers!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.