1
votes

I'm trying an example I found here where data is plotted using ggplot2. The code looks like this:

raw_text %>%
  group_by(newsgroup) %>%
  summarize(messages = n_distinct(id)) %>%
  ggplot(aes(messages, newsgroup)) +
  geom_col() +
  labs(y = NULL)

The bars inside the diagram are supposed to be horizontal, i.e. from left to right, but for me they are vertical:

enter image description here

What do I have to change to also get horizontal bars?

2
can you try updating your ggplot. would work better if you provide the example data. i have seen this once with an old version of ggplot2 - StupidWolf
Updating various packages solved the problem! - user1406177

2 Answers

1
votes

I would try this:

#Code
raw_text %>%
  group_by(newsgroup) %>%
  summarize(messages = n_distinct(id)) %>%
  ggplot(aes(y=messages, x=newsgroup)) +
  geom_col() +
  labs(y = NULL)+
  coord_flip()
1
votes

I reproduced the example you indicated by downloading the data. My plot looks like:

enter image description here

code as given:

library(dplyr)
library(tidyr)
library(purrr)
library(readr)
library(ggplot2)

# dataset has to be downloaded see question user1406177
training_folder <- "data/20news-bydate-train/"

# Define a function to read all files from a folder into a data frame
read_folder <- function(infolder) {
  tibble(file = dir(infolder, full.names = TRUE)) %>%
    mutate(text = map(file, read_lines)) %>%
    transmute(id = basename(file), text) %>%
    unnest(text)
}

raw_text <- tibble(folder = dir(training_folder, full.names = TRUE)) %>%
  mutate(folder_out = map(folder, read_folder)) %>%
  unnest(cols = c(folder_out)) %>%
  transmute(newsgroup = basename(folder), id, text)

raw_text %>%
  group_by(newsgroup) %>%
  summarize(messages = n_distinct(id)) %>%
  ggplot(aes(messages, newsgroup)) +
  geom_col() +
  labs(y = NULL)