Как я могу сделать гистограмму (гистограмму) для категорий с громкими именами в R? - PullRequest
0 голосов
/ 26 сентября 2019

Я хочу сделать следующую гистограмму в R: enter image description here

У меня есть следующий код:

antropico <- c(4,111,40,19,345,13,487,4,450,445,13,15)

barplot(antropico,
        xlab = "Frecuencia de ocurrencia",
        ylab = "Factor antrópico",
        names.arg = c("Accidente Industrial", "Accidente materiales riesgosos", "Accidente recreacional", "Accidente simple", "Accidente transporte", "Alteración de infraestructura", "Alteración suministro servicio báisco", "Explosión", "Incendio estructural", "Incendio Forestal", "Incendio transporte", "Incendio vertedero\relleno sanitario"),
        col = "blue",
        horiz = TRUE)

Но дело в том, что имена слишком велики, и R выдает мне предупреждение: Ошибка в plot.new (): слишком большие поля рисунка.У меня также есть проблемы с нанесением чисел на решетку, любая идея?Заранее спасибо.

PD: Все должно быть на испанском, так что если у кого-то есть решение, которое не будет работать, если метки категорий на испанском языке, так что спасибо, но этот язык для меня обязателен.

1 Ответ

1 голос
/ 26 сентября 2019

Вы можете сделать это, используя ggplot

library(tidyverse)
# put all in a dataframe
ant_df <- data.frame(names= c("Accidente Industrial", "Accidente materiales riesgosos", "Accidente recreacional", "Accidente simple", "Accidente transporte", "Alteración de infraestructura", "Alteración suministro servicio báisco", "Explosión", "Incendio estructural", "Incendio Forestal", "Incendio transporte", "Incendio vertedero\relleno sanitario"),
                 values=c(4,111,40,19,345,13,487,4,450,445,13,15))

ggplot(ant_df,aes(x=names,y=values)) + 
    geom_bar(stat="identity",fill="blue") +  # add bars
    coord_flip() + # flip x and y axis
    ylab("Frecuencia de ocurrencia") + xlab("Factor antrópico") + # add axis labels
     geom_text(aes(x = names,   y = values+30, label = values)) # add text labels

resulting plot

...