繁体   English   中英

ggplot2:单杠

[英]ggplot2: Horizontal bars

我正在尝试我在这里找到的一个示例,其中使用 ggplot2 绘制数据。 代码如下所示:

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

图中的条应该是水平的,即从左到右,但对我来说它们是垂直的:

在此处输入图像描述

我必须改变什么才能获得单杠?

我会试试这个:

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

我通过下载数据复制了您指定的示例。 我的 plot 看起来像:

在此处输入图像描述

给出的代码:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM