R geom_col不显示'bars'

时间:2018-01-16 20:44:21

标签: r ggplot2 plotly

关于在geom_col()图中显示实际条形,我遇到了这个奇怪的错误。

假设我有一个数据集(称为user_data),其中包含为特定用户(以及大量其他列)完成的更改总数(“调整”)的计数。让我们说它看起来像这样:

   User_ID  total_adjustments  additional column_1  additional column_2 ...
1  'Blah_17'     21                random_data          random_data
2  'Blah_1'      47                random_data          random_data
3  'foobar'      2                 random_data          random_data  
4  'acbd1'       17                random_data          random_data 
5  'user27'      9                 random_data          random_data  

我正在使用以下代码将其缩减为仅包含我关注的两列的数据框:

total_adj_count = user_data %>% 
  select(User_ID, total_adjustments) %>% 
  arrange(desc(total_adjustments)) %>% 
  mutate(User_ID = factor(User_ID, User_ID))

这导致我的数据框( total_adj_count )看起来像这样:

   User_ID  total_adjustments
1  'Blah_1'      47                  
2  'Blah_17'     21
3  'acbd1'       17
4  'user27'      9
5  'foobar'      2

继续,这是我用来尝试创建该数据的geom_col()图的代码:

g = ggplot(data=total_adj_count, aes(x = User_ID, y = total_adjustments)) +
  geom_bar(width=.5, alpha=1, show.legend = FALSE, fill="#000066", stat="identity") +
  labs(x="", y="Adjustment Count", caption="(based on sample data)") +
  theme_few(base_size = 10) + scale_color_few() +
  theme(axis.text.x=element_text(angle = 45, hjust = 1)) +
  geom_text(aes(label=round(total_adjustments, digits = 2)), size=3, nudge_y = 2000) +
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank())

p = ggplotly(g)

p = p %>% 
  layout(margin = m,
         showlegend = FALSE,
         title = "Number of Adjustments per User"
  )

p

由于某些奇怪的原因,当我尝试查看绘图 p 时,它会按预期显示绘图的所有部分,但不会显示实际的条形图(或列)。

事实上,我得到了这个奇怪的情节,并且有点困在哪里解决它: my plot

1 个答案:

答案 0 :(得分:2)

nudge_y参数更改为较小的数字。现在你将它设置为2000,在y轴上将标签偏移2000。下面我将其更改为nudge_y = 2,看起来如此:

g <- 
  ggplot(total_adj_count, aes(User_ID, total_adjustments)) +
  geom_col(width = .5, alpha = 1, show.legend = FALSE, fill = "#000066") +
  labs(x = "", y = "Adjustment Count", caption = "(based on sample data)") +
  theme_few(base_size = 10) + 
  scale_color_few() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  geom_text(aes(label = round(total_adjustments, digits = 2)), size = 3, nudge_y = 2) +
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
    )

enter image description here

完整复制/粘贴:

library(ggplot2)
library(ggthemes)
library(plotly)
library(dplyr)

text <- "  User_ID  total_adjustments
1  'Blah_1'      47                  
2  'Blah_17'     21
3  'acbd1'       17
4  'user27'      9
5  'foobar'      2"

total_adj_count <- read.table(text = text, header = TRUE, stringsAsFactors = FALSE)

g <-  
  ggplot(total_adj_count, aes(User_ID, total_adjustments)) +
  geom_col(width = .5, alpha = 1, show.legend = FALSE, fill = "#000066") +
  labs(x = NULL, y = "Adjustment Count", caption = "(based on sample data)", title = "Number of Adjustments per User") +
  theme_few(base_size = 10) + 
  scale_color_few() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  geom_text(aes(label = round(total_adjustments, digits = 2)), size = 3, nudge_y = 2) +
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  )

p <- ggplotly(g)
p <- layout(p, showlegend = FALSE)

p