I have a ggplot inside of a renderPlot function. This code block works as expected:
output$revenue_channel <- renderPlot({
ggplot(untrended_data(), aes(x = reorder(Channel, Revenue), y = Revenue), label = Revenue) +
geom_bar(stat="identity", fill = "#008080", alpha = 0.6) +
coord_flip() +
geom_text(aes(label = scales::dollar(Revenue)), hjust= 1.2, color = "white") +
scale_y_continuous(label = scales::label_dollar(scale = 0.001, suffix = "K")) +
xlab("") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
}, height = 300)
This code block as is runs and produces this chart:
But, in my I actually have a breakdown selector where the user ca enter one of Channel, Device or UserType.
So, this line:
ggplot(untrended_data(), aes(x = reorder(Channel, Revenue), y = Revenue), label = Revenue)
Would become either this:
ggplot(untrended_data(), aes(x = reorder(Device, Revenue), y = Revenue), label = Revenue)
Or this:
ggplot(untrended_data(), aes(x = reorder(UserType, Revenue), y = Revenue), label = Revenue)
I tried just switching in input$myinput like so
ggplot(untrended_data(), aes(x = reorder(input$breakdown, Revenue), y = Revenue), label = Revenue)
But this gives an error:
Error: arguments must have same length
I then tried aes_ for aes_string():
ggplot(untrended_data(), aes_(x = reorder(input$breakdown, "Revenue"), y = "Revenue"), label = Revenue)
Discrete value supplied to continuous scale
I then tried t make sense of this page on quasi-quotation and tried:
ggplot(untrended_data(), aes(x = reorder(!! input$breakdown, Revenue), y = Revenue), label = Revenue)
Which resulted in:
Error: arguments must have same length
How can I pass input$breakdown into ggplot within renderPlot({})?
The input$breakdown stores a string as value, and if we convert it to a symbol (using rlang::sym) and then evaluate (!!) it would work, e.g.
library(ggplot2)
v1 <- "mpg";
ggplot(mtcars, aes(x = reorder(!! rlang::sym(v1), cyl), y = cyl, label = cyl))+
geom_bar(stat="identity", fill = "#008080", alpha = 0.6) +
coord_flip()
In the OP's code block, we need to use reorder(!! rlang::sym(input$breakdown), Revenue)
output$revenue_channel <- renderPlot({
ggplot(untrended_data(), aes(x = reorder(!! rlang::sym(input$breakdown), Revenue),
y = Revenue), label = Revenue) +
geom_bar(stat="identity", fill = "#008080", alpha = 0.6) +
coord_flip() +
geom_text(aes(label = scales::dollar(Revenue)), hjust= 1.2, color = "white") +
scale_y_continuous(label = scales::label_dollar(scale = 0.001, suffix = "K")) +
xlab("") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
}, height = 300)
Related
So my legend here is village which has (Chirodzo, God, Ruaca). How to remove the legend and display it inside the bars; for instance inside the bar for chirodzo, I want chirodzo written inside?
ggplot(data = interviews_plotting, aes(x = respondent_wall_type, fill = village)) +
geom_bar(position = "fill")
Source is here https://mq-software-carpentry.github.io/r-ggplot-extension/02-categorical-data/index.html
ggplot(data = interviews_plotting, aes(x = respondent_wall_type, fill = village)) +
geom_bar(position = "fill")
To label your bars with the fill category and getting rid of the legend you could use geom_text like so:
Using mtcars as example data:
library(ggplot2)
ggplot(data = mtcars, aes(x = am, fill = factor(cyl))) +
geom_bar(position = "fill") +
geom_text(aes(label = cyl), stat = "count", position = position_fill(vjust = 0.5)) +
guides(fill = "none")
From your comments, it sounds like you are looking for something like this:
library(ggplot2)
ggplot(interviews_plotting, aes(x = respondent_wall_type, fill = village)) +
geom_bar(position = position_dodge()) +
geom_text(stat = 'count',
aes(y = stat(count)/2, label = village, group = village),
position = position_dodge(width = 1), angle = 90) +
guides(fill = guide_none())
Or, if you want to get a bit more sophisticated with your label placement and theme choices:
library(ggplot2)
ggplot(interviews_plotting, aes(x = respondent_wall_type, fill = village)) +
geom_bar(position = position_dodge(width = 0.9), width = 0.8) +
geom_text(stat = 'count', size = 6,
aes(y = ifelse(stat(count) > 2, stat(count)/2, stat(count)),
label = village, group = village,
hjust = ifelse(stat(count) > 2, 0.5, -0.2)),
position = position_dodge(width = 0.9), angle = 90) +
labs(x = 'Wall type', y = 'Count') +
theme_minimal(base_size = 16) +
scale_fill_brewer(palette = 'Set2', guide = 'none')
Data used
interviews_plotting <- read.csv(paste0("https://raw.githubusercontent.com/",
"humburg/r-ggplot-project/master/",
"data_output/interviews_plotting.csv"))
I am creating a plot with 3 variables as below. Is there a way to arrange the plot in a descending order such that the bar with the highest proportion of variable "c" comes first in the plot. Using this example last bar should come in first then middle one and then the first bar in the last.
long<- data.frame(
Name = c("abc","abc","abc","gif","gif","gif","xyz","xyz","xyz"),
variable = c("a","b","c","a","b","c","c","b","a"),
value = c(4,6,NA,2,8,1,6,NA,NA))
long_totals <- long %>%
group_by(Name) %>%
summarise(Total = sum(value, na.rm = T))
p <- ggplot()+
geom_bar(data = long,
aes(x = Name,
y = value,
fill=variable),
stat="summary",
position = "fill") +
geom_text(data = long_totals,
aes(y = 100,
x = Name,
label = Total),
size = 7,
position = position_fill(vjust = 1.02)) +
scale_y_continuous(labels = scales::percent_format()) +
ylab("Total_num") +
ggtitle("Totalnum") +
theme(plot.title = element_text(size = 20, hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 75, vjust = 0.95, hjust=1))
The following code does arrange the bars by count of "c" but not by proportion. How can I arrange by proportion?
p<-long %>%
mutate(variable = fct_relevel(variable,
c("c", "b", "a"))) %>%
arrange(variable) %>%
mutate(Name = fct_inorder(Name))
p %>%
ggplot() +
aes(x = Name,
y = value,
fill = variable) +
geom_bar(position = "fill",
stat = "summary") +
We could use fct_rev from forcats package, it is in tidyverse:
p <- ggplot()+
geom_bar(data = long,
aes(x = fct_rev(Name),
y = value,
fill=variable),
stat="summary",
position = "fill") +
geom_text(data = long_totals,
aes(y = 100,
x = Name,
label = Total),
size = 7,
position = position_fill(vjust = 1.02)) +
scale_y_continuous(labels = scales::percent_format()) +
ylab("Total_num") +
ggtitle("Totalnum") +
theme(plot.title = element_text(size = 20, hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 75, vjust = 0.95, hjust=1))
i need the plan legend
How to add a legend manually for geom_line
ggplot(data = impact_end_Current_yr_m_actual, aes(x = month, y = gender_value)) +
geom_col(aes(fill = gender))+theme_classic()+
geom_line(data = impact_end_Current_yr_m_plan, aes(x=month, y= gender_value, group=1),color="#288D55",size=1.2)+
geom_point(data = impact_end_Current_yr_m_plan, aes(x=month, y=gender_value))+
theme(axis.line.y = element_blank(),axis.ticks = element_blank(),legend.position = "bottom", axis.text.x = element_text(face = "bold", color = "black", size = 10, angle = 0, hjust = 1))+
labs(x="", y="End Beneficiaries (in Num)", fill="")+
scale_fill_manual(values=c("#284a8d", "#00B5CE","#0590eb","#2746c2"))+
scale_y_continuous(labels = function(x) format(x, scientific = FALSE)
The neatest way to do it I think is to add colour = "[label]" into the aes() section of geom_line() then put the manual assigning of a colour into scale_colour_manual() here's an example from mtcars (apologies that it uses stat_summary instead of geom_line but does the same trick):
library(tidyverse)
mtcars %>%
ggplot(aes(gear, mpg, fill = factor(cyl))) +
stat_summary(geom = "bar", fun = mean, position = "dodge") +
stat_summary(geom = "line",
fun = mean,
size = 3,
aes(colour = "Overall mean", group = 1)) +
scale_fill_discrete("") +
scale_colour_manual("", values = "black")
Created on 2020-12-08 by the reprex package (v0.3.0)
The limitation here is that the colour and fill legends are necessarily separate. Removing labels (blank titles in both scale_ calls) doesn't them split them up by legend title.
In your code you would probably want then:
...
ggplot(data = impact_end_Current_yr_m_actual, aes(x = month, y = gender_value)) +
geom_col(aes(fill = gender))+
geom_line(data = impact_end_Current_yr_m_plan,
aes(x=month, y= gender_value, group=1, color="Plan"),
size=1.2)+
scale_color_manual(values = "#288D55") +
...
(but I cant test on your data so not sure if it works)
I want to add significance stars for mean difference comparisons to a plot. Without the lines for the stars, the plot works:
da<-data.frame(group=c("condition1_high","condition1_low","condition2_high","condition2_low"),numb=c(30,25,26,20))
da %>% separate(group, c("A", "B"), remove = F) %>%
ggplot(aes(x=A, y=numb, fill = B)) +
geom_bar(position=position_dodge(), stat="identity") +
scale_fill_manual(values=rep(c("grey20","grey80"), ceiling(length(da$group)/2))[1:length(da$group)]) +
geom_text(aes(label=numb),
position = position_dodge(width = 0.9), vjust = -0.25) +
geom_signif(stat="identity",
data=data.frame(x=c(0.5,1.5), xend=c(1,2),
y=c(30, 30), annotation=c("**", "*","***","+")),
aes(x=x,xend=xend, y=y, yend=y, annotation=annotation))
Now I add a bit of code for the stars I found here on this platform:
da %>% separate(group, c("A", "B"), remove = F) %>%
ggplot(aes(x=A, y=numb, fill = B)) +
geom_bar(position=position_dodge(), stat="identity") +
scale_fill_manual(values=rep(c("grey20","grey80"), ceiling(length(da$group)/2))[1:length(da$group)]) +
geom_text(aes(label=numb),
position = position_dodge(width = 0.9), vjust = -0.25) +
geom_signif(stat="identity",
data=data.frame(x=c(0.5,1.5), xend=c(1,2),
y=c(30, 30), annotation=c("**", "*")),
aes(x=x,xend=xend, y=y, yend=y, annotation=annotation))
Now it says that object B is missing. What can I do?
You need to add inherit.aes = FALSE to the geom_signif call, otherwise it will try to find a column called B in the new data frame you defined. This is because you put an aes call inside your initial call to ggplot. When you do this, by default all subsequent geoms will inherit the aesthetics and data from this call. If you pass new data to a geom, it needs to include a value for all those aesthetics or override the aesthetics or you need to switch off inheritance with inherit.aes = FALSE
da %>%
separate(group, c("A", "B"), remove = FALSE) %>%
ggplot(aes(x = A, y = numb, fill = B)) +
geom_bar(position=position_dodge(), stat = "identity") +
scale_fill_manual(values = rep(c("grey20", "grey80"),
ceiling(length(da$group)/2))[1:length(da$group)]) +
geom_text(aes(label=numb),
position = position_dodge(width = 0.9), vjust = -0.25) +
geom_signif(stat="identity", inherit.aes = FALSE,
data=data.frame(x = c(0.5, 1.5), xend=c(1, 2),
y = c(30, 30), annotation = c("**", "*")),
aes(x = x, xend = xend, y = y, yend = y, annotation = annotation))
I would like to change the colour of one of my ggrepel labels to black. I have tried to override the inheritance by specifying ...geom_text_repel(...colour='black') but that doesn't seem to work.
My attempt at a fix to the problem is in the second geom_text_repel function (below).
N.B. If there is a way to control the colour of individual geom_text_repel elements, rather than having to call the function twice, I would prefer that.
library("tidyverse")
library("ggthemes")
library("ggrepel")
df1 <- gather(economics, variable_name, observation, -date) %>%
rename(period = date) %>%
filter(variable_name == 'psavert')
df2 <- gather(economics, variable_name, observation, -date) %>%
rename(period = date) %>%
filter(variable_name == 'uempmed')
ggplot(df1, aes(x = period, y = observation, colour = variable_name)) +
geom_line() +
geom_line(data = df2, colour = 'black', size = .8) +
geom_text_repel(
data = subset(df1, period == max(as.Date(period, "%Y-%m-%d"))),
aes(label = variable_name),
size = 3,
nudge_x = 45,
segment.color = 'grey80'
) +
geom_text_repel(
data = subset(df2, period == max(as.Date(period, "%Y-%m-%d"))),
aes(label = variable_name, colour = 'black'), #How do I set the colour of the label text to black?
size = 3,
nudge_x = 45,
segment.color = 'grey80'
) +
scale_y_continuous(labels = scales::comma) +
theme_minimal(base_size = 16) +
scale_color_tableau() +
scale_fill_tableau() +
theme(legend.position = 'none') +
labs(x="", y="", title = "Economic Data") +
scale_x_date(limits = c(min(df1$period), max(df1$period) + 1200))
Do the same thing you did in your geom_line() layer. You want to set a color, not a mapping. Make colour = 'black' an argument to geom_text_repel(), not aes().
ggplot(df1, aes(x = period, y = observation, colour = variable_name)) +
geom_line() +
geom_line(data = df2, colour = 'black', size = .8) + # just like this layer
geom_text_repel(
data = subset(df1, period == max(as.Date(period, "%Y-%m-%d"))),
aes(label = variable_name),
size = 3,
nudge_x = 45,
segment.color = 'grey80'
) +
geom_text_repel(
data = subset(df2, period == max(as.Date(period, "%Y-%m-%d"))),
aes(label = variable_name) # don't assign it here,
size = 3,
nudge_x = 45,
segment.color = 'grey80',
colour = "black" # assign it here
) +
scale_y_continuous(labels = scales::comma) +
theme_minimal(base_size = 16) +
scale_color_tableau() +
scale_fill_tableau() +
theme(legend.position = 'none') +
labs(x="", y="", title = "Economic Data") +
scale_x_date(limits = c(min(df1$period), max(df1$period) + 1200))
Note that now the first line AND text are now both set manually to "black", so the automatic variable assignment will start over with next line (and text). If you want to set that manually to a different color, you can use the same strategy (set it as an argument to the geom, not as an argument to aes