Couch Tomato
Couch Tomato

Reputation: 85

What's the syntax for this kind of graph using ggplot in R?

Here is the graph

How can I make a graph like that? Thank you.

If I have two dataframes like this here, and their topics are the same.

Upvotes: 0

Views: 69

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173793

You could do something like this:

library(ggplot2)

df <- data.frame(Frequency = c(300, 0, 600, 900, 0, 1000, 700, 
                           0, 300, 400, 0, 1400, 1600, 6500,
                           0, -250, -500, -400, -600, -1300, 
                           0, -1150, -100, 0, -200, 0, -2500, 
                           -2500),
                 Stream = rep(c("Ne", "Pri"), each = 14),
                 country = c("Argentina", "Brazil", "Canada", 
                           "France", "Germany", "India", 
                           "Indonesia", "Italy", "Mexico", 
                           "Phillipines", "Spain", "Thailand", 
                           "United Kingdom", "United States"))

ggplot(df, aes(Frequency, country, fill = Stream)) + 
  geom_col(width = 0.6) +
  labs(y = "") +
  scale_x_continuous(breaks = c(-2500, 0, 2500, 3750, 5000, 6250),
                     labels =c(250, 0, 2500, 3750, 5000, 6250),
                     limits = c(-2600, 7000)) +
  theme_bw() +
  theme(panel.border = element_blank())

enter image description here


Edit

If I have two data frames like this:

df1 <- data.frame(topic = c("design", "game", "hardware", "price"),
                  n = c(80, 1695, 29, 53))

df1
#>      topic    n
#> 1   design   80
#> 2     game 1695
#> 3 hardware   29
#> 4    price   53

df2 <- data.frame(topic = c("design", "game", "hardware", "price"),
                  n = c(400, 1235, 290, 107))

df2
#>      topic    n
#> 1   design  400
#> 2     game 1235
#> 3 hardware  290
#> 4    price  107

Then I can simply rbind them together, negating the n column on df2 first and adding a column to show which data frame each value came from:

df3 <- rbind(df1, within(df2, n <- -n))
df3$origin <- rep(c("df1", "df2"), each = nrow(df1))

And when I plot, I add abs as a labeller in `scale_x_continuous to remove the negative symbols on the left half of the plot.

ggplot(df3, aes(n, topic, fill = origin)) +
  geom_col() +
  scale_x_continuous(labels = abs)

Upvotes: 1

Related Questions