DGav
DGav

Reputation: 281

R geom_col does not show the 'bars'

I am having this strange error regarding displaying the actual bars in a geom_col() plot.

Suppose I have a data set (called user_data) that contains a count of the total number of changes ('adjustments') done for a particular user (and a plethora of other columns). Let's say it looks like this:

   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  

I am using the following code to reduce it into a dataframe with only the two columns I care about:

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

This results in my dataframe (total_adj_count) looking like so:

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

Moving along, here is the code I used to attempt to create a geom_col() plot of that data:

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

And for some strange reason when I try to view plot p it displays all parts of the plot as intended, but does not show the actual bars (or columns).

In fact I get this strange plot and am sort of stuck where to fix it: my plot

Upvotes: 0

Views: 1846

Answers (1)

tyluRp
tyluRp

Reputation: 4768

Change nudge_y argument to a smaller number. Right now you have it set to 2000 which offsets the labels by 2000 on the y-axis. Below I've changed it to nudge_y = 2 and it looks like so:

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

Full copy/paste:

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

Upvotes: 2

Related Questions