R: как сделать одну столбчатую диаграмму в ggplot2 - PullRequest
0 голосов
/ 27 августа 2018
Ancestry <- data.frame(Race = c("European", "African American", "Asian", "Hispanic", "Other"), Proportion = c(40, 30, 10, 15, 5))
Ancestry %>% 
ggplot(aes(y = Proportion, fill = Race)) + 
  geom_bar(stat="identity", colour="white")

Запуск приведенного выше кода дает мне следующую ошибку:

Warning in min(x, na.rm = na.rm) :
  no non-missing arguments to min; returning Inf
Warning in max(x, na.rm = na.rm) :
  no non-missing arguments to max; returning -Inf
Warning in min(diff(sort(x))) :
  no non-missing arguments to min; returning Inf
Warning in x - width/2 :
  longer object length is not a multiple of shorter object length
Warning in x + width/2 :
  longer object length is not a multiple of shorter object length
Error in data.frame(list(y = c(40, 30, 10, 15, 5), fill = c(3L, 1L, 2L,  : 
  arguments imply differing number of rows: 5, 2947

Я пытаюсь создать диаграмму с накоплением, подобную этой:

enter image description here

Ответы [ 2 ]

0 голосов
/ 27 августа 2018

Мы можем создать фиктивную переменную, чтобы сгруппировать все записи в одну группу, а затем использовать geom_bar для создания столбчатой ​​диаграммы с накоплением и использовать geom_text для присвоения имени Proportions.

library(ggplot2)

#Dummy group variable
Ancestry$row <- 1

#Create a label variable
Ancestry$percent <- (Ancestry$Proportion/sum(Ancestry$Proportion)) * 100

ggplot(Ancestry, aes(x = row,y = Proportion, fill = Race)) +
    geom_bar(stat="identity") + 
    geom_text(aes(label = percent), 
        position = position_stack(vjust = 0.5))

enter image description here

0 голосов
/ 27 августа 2018

Вам необходимо создать фиктивную переменную для оси X.Затем используйте geom_col, который похож на geom_bar(stat = "identity"), чтобы построить столбчатую диаграмму с накоплением + geom_text, чтобы поместить текст на полосу.

Показанный вами график использовал theme_economist из пакета ggthemes.

library(tidyverse)

Ancestry <- data.frame(Race = c("European", "African American", "Asian", "Hispanic", "Other"), 
                       Proportion = c(40, 30, 10, 15, 5))

Ancestry <- Ancestry %>% 
  mutate(Year = "2006")

ggplot(Ancestry, aes(x = Year, y = Proportion, fill = Race)) +
  geom_col() +
  geom_text(aes(label = paste0(Proportion, "%")),
            position = position_stack(vjust = 0.5)) +
  scale_fill_brewer(palette = "Set2") +
  theme_minimal(base_size = 16) +
  ylab("Percentage") +
  xlab(NULL)

library(ggthemes)

ggplot(Ancestry, aes(x = Year, y = Proportion, fill = Race)) +
  geom_col() +
  geom_text(aes(label = paste0(Proportion, "%")),
            position = position_stack(vjust = 0.5)) +
  theme_economist(base_size = 14) +
  scale_fill_economist() +
  theme(legend.position = "right", 
        legend.title = element_blank()) +
  theme(axis.title.y = element_text(margin = margin(r = 20))) +
  ylab("Percentage") +
  xlab(NULL)

Создано в 2018-08-26 пакетом Представить (v0.2.0.9000).

...