Reputation: 2597
I'd like to set the text on each of the columns in ggplot and they're bunching up altogether
this is my code
set.seed(1)
gg <-
iris[sample(300, 50), ] %>%
ggplot(aes(Species, Sepal.Length, label = Sepal.Length, fill = as.factor(Sepal.Width > 3))) +
geom_col(position = "dodge2") +
geom_text(position=position_dodge(width = .7),
vjust=-0.25)
ggplotly(gg)
Upvotes: 0
Views: 1188
Reputation: 39595
Maybe I would suggest using same position style in geom_col()
and geom_text()
:
library(plotly)
library(ggplot2)
set.seed(1)
#Plot
gg <-
iris[sample(300, 50), ] %>%
ggplot(aes(Species, Sepal.Length, label = Sepal.Length, fill = as.factor(Sepal.Width > 3))) +
geom_col(position = position_dodge2(0.9)) +
geom_text(position=position_dodge2(width = .9),
vjust=-0.5)
#Transform
ggplotly(gg)
Output:
With shared data, try this, you have to format the date to have dodged labels:
library(dplyr)
library(ggplot2)
library(plotly)
#Code
gg <- df %>%
mutate(first_month=factor(format(first_month,'%b-%m'),
levels = unique(format(first_month,'%b-%m')),
ordered = T)) %>%
ggplot(aes(x=first_month, y=customers,
label = customers, fill = plan_id,group=plan_id)) +
geom_bar(stat='identity',position = 'dodge')+
geom_text(aes(group=plan_id),position = position_dodge(0.9),vjust = -0.5)
#Plot 2
ggplotly(gg)
Output:
Upvotes: 1