how to add a legend in a ggplot? - r

enter image description hereI am having a problem with my ggplot that i cannot insert a legend. I just want to show the total number of facilities per region (manually).
Here is my code:
Note: my csv file has 17,333 IDs, I was thinking maybe that is why but I'm not really sure so.
library(ggplot2)
library(dplyr)
library(ggthemes)
library(tidyverse)
doh = read.csv("doh.csv")
doh %>%
ggplot(aes( y = region, color = region)) +
geom_bar(position = "identity", size = 0.7, alpha = 0.8, fill = "#28d1eb", colour="black") +
labs(title = "Total Number of COVID-19 Facilities per Region",
x = "Count",
y = "Region") +
theme_minimal() +
theme(plot.title = element_text(lineheight=6, face="bold", color="black",size=15))
I have tried inserting a legend in my code but it isn't working and I'm not sure where I went wrong.
code:
library(ggplot2)
library(dplyr)
library(ggthemes)
library(tidyverse)
doh = read.csv("doh.csv")
doh %>%
ggplot(aes( y = region, fill = region, color = region)) +
geom_bar(position = "identity", size = 1.0, alpha = 0.8, fill = "#28d1eb", colour="black") +
labs(title = "Total Number of COVID-19 Facilities per Region") +
theme_minimal() +
theme(plot.title = element_text(lineheight=6, face="bold", color="black",size=15)) +
barplot(data,
col = c("#f2b50c", "#d96fe3")) +
legend("topright",
legend = c("REGION XIII (CARAGA)"))
PS: I only included the "REGION XIII (CARAGA)" because I just want to see if its working but its not.
enter image description here
Thanks in adv!

Here is a way. Count the regions before piping to ggplot.
The example below uses the data set diamonds, substitute region for clarity and the code should work.
suppressPackageStartupMessages({
library(ggplot2)
library(dplyr)
})
data(diamonds)
diamonds %>%
select(clarity) %>%
count(clarity, name = "Count") %>%
ggplot(aes(x = clarity, y = Count, fill = clarity)) +
geom_col(alpha = 0.8, fill = "#28d1eb", colour = "black") +
geom_text(aes(label = Count), hjust = -0.1) +
coord_flip() +
labs(title = "Total Number of COVID-19 Facilities per Region") +
theme_minimal(base_size = 15) +
theme(plot.title = element_text(lineheight = 6, face = "bold", color="black"))
Created on 2022-11-21 with reprex v2.0.2

Related

Creating manually a legend in ggplot R [duplicate]

This question already has an answer here:
How to insert a legend in a GGPLOT with multiple time series
(1 answer)
Closed 2 years ago.
So this is the graph and the code I have so far:
library(fivethirtyeight)
library(tidyverse)
bad_drivers$num_drivers
bad_drivers$perc_speeding
mytable <- bad_drivers %>%
mutate(SpeedPerBilion = (num_drivers * perc_speeding)/100)
ggplot(data = mytable, aes(x = state, y = SpeedPerBilion, fill='red')) +
xlab("") +
ylab("") +
coord_flip() +
geom_bar(stat = "identity")+
geom_bar(data= mytable, aes(x = state, y=num_drivers), alpha=0.5,stat="identity") +
theme(plot.title =`enter code here` element_text(face = "bold"), legend.position = "none")+
scale_y_continuous(sec.axis = dup_axis())+
labs(title = "Drviers Involved In Fatal Collisions While Speeding",
subtitle = "As a share of the number of fatal collisions per billion miles, 2009")
So my questions are:
How am I adding a legend to this graph?
How to erase this lower y coordinate (so to have just the upper one)?
Thank you in advance! :)
Find code below and what I believe is your desired plot. You will have to tweak labels to match what you need but I put place holder names. The key is using the scale_fill_manual with a named vector of colors and calling those color names in the aes of each layer you need to use that color in. Also a neat trick is using alpha() to apply alpha as a color rather than a separate scale. Finally the y axis transformation you were looking for is position = "right" so it ends up on top after coord_flip().
library(fivethirtyeight) # for data
library(tidyverse)
bad_drivers %>%
mutate(SpeedPerBilion = (num_drivers * perc_speeding)/100) %>%
ggplot(aes(x = state, y = SpeedPerBilion)) +
xlab("") +
ylab("") +
coord_flip() +
geom_bar(stat = "identity", aes(fill = "Speeding")) +
geom_bar(aes(x = state, y = num_drivers, fill = "All"),
stat = "identity") +
theme(plot.title = element_text(face = "bold")) +
scale_y_continuous(position = "right") +
scale_fill_manual(name = "Speeding Involved",
values = c("Speeding" = alpha("red", 1), "All" = alpha("red", 0.5))) +
labs(title = "Drviers Involved In Fatal Collisions While Speeding",
subtitle = "As a share of the number of fatal collisions per billion miles, 2009") +
guides(fill = guide_legend(override.aes = list(color = "red", alpha = c(0.25, 1))))
Created on 2022-10-11 by the reprex package (v2.0.1)
Note: For some reason the transparency in the legend doesn't look the same as in the plot so I manually set the legend to alpha = 0.25 so to my eye it matches the plot. Please confirm the result on your own computer.
Maybe this:
library(fivethirtyeight)
library(tidyverse)
mytable <- bad_drivers %>%
mutate(SpeedPerBilion = (num_drivers * perc_speeding)/100)
ggplot(data = mytable, aes(x = state, y = SpeedPerBilion, fill='red')) +
xlab("") +
ylab("") +
coord_flip() +
geom_bar(stat = "identity")+
geom_bar(data= mytable, aes(x = state, y=num_drivers), alpha=0.5,stat="identity") +
scale_y_continuous(sec.axis = dup_axis())+
theme(plot.title = element_text(face = "bold"),
axis.text.x.bottom = element_blank(),
axis.ticks.x.bottom = element_blank())+
labs(title = "Drviers Involved In Fatal Collisions While Speeding",
subtitle = "As a share of the number of fatal collisions per billion miles, 2009")
Output:

How to add a legend manually for line chart

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)

ggplot2 with side by side and proportional fill

I have data that looks like this:
My goal is to have a barplot grid as follows: Each plot will be specific to 1 race_ethnicity group. The x-axis in each plot will be the different age_bin groups. For each age_bin, there will be two bars: 1 for men, and 1 for women. For each bar, I want it to be filled with the proportion of Likely/(Unlikely + Likely). Preferably, each bar would have a height of 1 and a line cut through it so Likely% of that bar is one color with a label. This is what I currently have:
I am running into issues with 1) using a predefined proportion as the fill, and 2) having two different "fills" (one for biological sex, one for the predefined proportion.
Thanks to anyone who can help with this. My code is currently the following:
ggplot(data=who_votes_data, aes(x=age_bin,y=1, fill=gender)) +
geom_bar(stat='identity',aes(fill = gender), position = position_dodge2()) +
facet_wrap(~race_ethnicity, nrow = 2, scales = "free") +
geom_text(aes(label=paste0(sprintf("%1.1f", prop*100),"%"), y=prop),
colour="white") +
labs(x = expression("Age Group"), y= ("Prortion of Likely Voters"),
title = "Proportion of Likely Voters Across Age Groups, Race/Ethnicity, and Sex",
caption="Figure 1") + theme(plot.caption = element_text(hjust = 0.5, vjust = -0.5, size = 18))
https://docs.google.com/spreadsheets/d/1a7433iwXNSwcuXDJOvqsxNDN6oaYULVlyw22E41JROU/edit?usp=sharing
Updated Code:
library(tidyverse)
library(ggplot2)
df<- read.csv("samplevotes.csv")
df %>%
group_by(race_ethnicity, age_bin, gender) %>%
summarise(Likely = sum(Likely),
Unlikely = sum(Unlikely),
proportion = Likely/(Likely+Unlikely)) %>% ungroup() %>%
ggplot(aes(x = age_bin, y = proportion, fill = gender)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~race_ethnicity, nrow = 2) +
geom_text(aes(label=paste0(sprintf("%1.1f", proportion*100),"%"), y=proportion), position = position_dodge(width = 1), colour="Black", size = 2.2) +
labs(x = expression("Age Group"), y= ("Proportion of Likely Voters"), title = "Proportion of Likely Voters Across Age Groups, Race/Ethnicity, and Sex", caption="Figure 1") +
theme(plot.caption = element_text(hjust = 0.5, vjust = -0.5, size = 18))
Here is the code I would use. I did make some changes based on the way the data was combined.
df %>%
group_by(race_ethnicity, age_bin, gender) %>%
summarise(Likely = sum(Likely),
Unlikely = sum(Unlikely),
proportion = Likely/(Likely+Unlikely)) %>% ungroup() %>%
ggplot(aes(x = age_bin, y = proportion, fill = gender)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~race_ethnicity, nrow = 2) +
geom_text(aes(label=paste0(sprintf("%1.1f", proportion*100),"%"), y=proportion), position = position_dodge(width = 1), colour="Black", size = 2.2) +
labs(x = expression("Age Group"), y= ("Proportion of Likely Voters"), title = "Proportion of Likely Voters Across Age Groups, Race/Ethnicity, and Sex", caption="Figure 1") +
theme(plot.caption = element_text(hjust = 0.5, vjust = -0.5, size = 18))
Here is what it looks like

How to add individual labels to points on a line graph in R

I am wanting to add labels pointing out the year of the 3 highest, and 3 lowest temperatures on a line graph showing changes in average temperature. I can't figure out how to do so for just those 6 points, instead of every point... Any help?
#load data up
library(readxl)
TempData <- read_excel("R Data/TempData.xlsx")
View(TempData)
#initiliase relevant packages #ggplot2 for creating data visulation and viridis to allow for colour gradients
library(ggplot2)
library(viridis)
#plot line graph
g1 <- ggplot(TempData, aes(x = Year, y = GAT, color = GAT)) +
geom_line(size = 1.5) +
geom_smooth(method=loess, se=TRUE, col = "black") +
scale_colour_gradient2(low = "green", mid = "yellow" , high = "red", midpoint=median(TempData$GAT)) +
labs(title = "Global Average Temperature", subtitle = "From 1850 to 2018") +
xlab("Year") + ylab ("Average Temperature") +
theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 16)) +
theme(plot.subtitle = element_text(face = "italic", hjust = 0.5, size = 10, colour = "Orange")) +
theme_light()
plot(g1)
As #Nate suggested, a common approach is to feed a subset of your data into geom_text. You could define those before ggplot2 or if it's simple, define those inline and feed into that layer's own data term. Here, I use dplyr::top_n to grab the top 3 and bottom 3 weights.
library(dplyr)
ggplot(mtcars, aes(x = wt, y = mpg, color = hp, label = mpg)) +
geom_line(size = 1.5) +
geom_smooth(method=loess, se=TRUE, col = "black") +
scale_colour_gradient2(low = "green", mid = "yellow" , high = "red",
midpoint=median(mtcars$hp)) +
geom_text(data = mtcars %>% top_n(3, wt),
hjust = 1.5, color = "black", angle = 90) +
geom_text(data = mtcars %>% top_n(-3, wt),
hjust = -0.5, color = "black", angle = 90) +
theme_light()
We don't have your data so I took your code and applied it to the standard mtcars dataset. (Not a great aesthetic match but you get the idea...)

How to add legend and table with data value into a chart with different lines using ggplot2

I have this data
TX_growth<-data.frame(year=c(2017,2016, 2015),statewide=c(61, 62,57),black=c(58,58,53),hispanic=c(59,60,55),white=c(65,64,61))
Until now I have this chart using the following code:
My chart until now
ggplot() + geom_line(data = TX_growth, aes(x=year, y= statewide), color = "blue", size=1) +
geom_line(data = TX_growth, aes(x=year, y= white), color = "red", size=1) +
geom_line(data = TX_growth, aes(x=year, y= black), color = "green", size=1) +
geom_line(data = TX_growth, aes(x=year, y= hispanic), color = "orange", size=1) +
labs(title = "Figure 1: Statewide Percent who Met or Exceeded Progress",
subtitle = "Greater percentage means that student subgroup progressed at higher percentage than previous year.",
x = "Year", y = "Percentage progress")+ theme_bw() +
scale_x_continuous(breaks=c(2017,2016,2015))
I want to add (a) legend showing the name and color of each line and (b) a table below with all values of my dataframe. Something like this:
What I want
Instead of cities, my chart would have "Statewide", "White", "Black", and "Hispanic". Also, my table would have years (from 2015 to 2017), rather than months. I don't want the seasons or "freezing" line. I just want to add the legend and table like they did it.
Part 1 - Fixing the legend
Concerning the legend, this is not the ggplot-way. Convert your data from wide to long, and then map the what keys to the colour as an aesthetic mapping.
library(tidyverse)
TX_growth %>%
gather(what, value, -year) %>%
ggplot() +
geom_line(aes(x=year, y= value, colour = what), size=1) +
labs(
title = "Figure 1: Statewide Percent who Met or Exceeded Progress",
subtitle = "Greater percentage means that student subgroup progressed at higher percentage than previous year.",
x = "Year", y = "Percentage progress") +
theme_bw() +
scale_x_continuous(breaks=c(2017,2016,2015))
Part 2 - Adding a table
Concerning the table, this seems to be somewhat of a duplicate of Adding a table of values below the graph in ggplot2.
To summarise from various posts, we can use egg::ggarrange to add a table at the bottom; here is a minimal example:
library(tidyverse)
gg.plot <- TX_growth %>%
gather(what, value, -year) %>%
ggplot() +
geom_line(aes(x=year, y= value, colour = what), size=1) +
theme_bw() +
scale_x_continuous(breaks=c(2017,2016,2015))
gg.table <- TX_growth %>%
gather(what, value, -year) %>%
ggplot(aes(x = year, y = as.factor(what), label = value, colour = what)) +
geom_text() +
theme_bw() +
scale_x_continuous(breaks=c(2017,2016,2015)) +
guides(colour = FALSE) +
theme_minimal() +
theme(
axis.title.y = element_blank())
library(egg)
ggarrange(gg.plot, gg.table, ncol = 1)
All that remains to do is some final figure polishing.
Part 3 - After some polishing ...
library(tidyverse)
gg.plot <- TX_growth %>%
gather(Group, value, -year) %>%
ggplot() +
geom_line(aes(x = year, y = value, colour = Group)) +
theme_bw() +
scale_x_continuous(breaks = 2015:2017)
gg.table <- TX_growth %>%
gather(Group, value, -year) %>%
ggplot(aes(x = year, y = as.factor(Group), label = value, colour = Group)) +
geom_text() +
theme_bw() +
scale_x_continuous(breaks = 2015:2017) +
scale_y_discrete(position = "right") +
guides(colour = FALSE) +
theme_minimal() +
theme(
axis.title.y = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
library(egg)
ggarrange(gg.plot, gg.table, ncol = 1, heights = c(4, 1))

Resources