Making legends with ggplot2 [duplicate] - r

This question already has answers here:
How to add legend to ggplot manually? - R [duplicate]
(1 answer)
Missing legend with ggplot2 and geom_line
(1 answer)
Closed 1 year ago.
I'm trying to plot some poll results but always have to add the legend manually after creating the plot. Here's my data (poll.csv):
Date,AKP,MHP,CHP,IYI,HDP,DEVA,GP,SP
01-05-2021,34.8,9,27.2,13.1,8.9,3.1,1.8,1.6
01-06-2021,36.2,9.1,20.3,16.7,10.5,3.6,1.3,0.6
01-14-2021,35,9.5,25,13,10.7,2.5,2.6,0.8
01-27-2021,35.6,8.5,24.9,13.6,8.9,2.3,2.5,1.6
01-30-2021,35.6,8.1,25.3,13.4,10.5,2.9,3,0.9
02-06-2021,35.8,7.8,24.2,12.7,11.4,4.6,1.3,0.8
02-08-2021,39.3,10.5,22.6,13.1,10.2,,,1.2
02-13-2021,35.2,9.6,21.1,15.6,10.6,3.8,1.7,1
02-13-2021,34.1,7.5,27.3,14,10.1,2.4,2.7,1.4
02-15-2021,35.2,9,26.5,12.9,9.9,2.5,1,1.5
02-23-2021,36.8,11.6,22.3,10.8,8.2,2.1,2.6,1.3
02-23-2021,41.7,11.6,21.8,10.3,9.3,1.8,0.4,0.8
02-26-2021,36.1,10,24.1,14,10.1,1.6,1.4,1.3
03-03-2021,34.8,7,26.2,13.1,9.1,2.5,2.7,0.7
03-06-2021,36.5,8.1,24,13.1,11,4,,
03-08-2021,36,9.9,26.3,13.5,8.7,2.1,1.9,1.1
03-10-2021,36,,29,,10.4,,,
03-11-2021,35.4,8.3,22,16.5,11.3,2.9,1.2,1.2
03-14-2021,34.4,7.2,26.2,13.9,9.6,2.7,2.8,0.8
and here's my code:
library(ggplot2)
library(anytime)
poll = read.csv("poll.csv")
poll$Date = as.Date(anydate(poll$Date))
ggplot(poll) +
geom_point(aes(x = Date, y = AKP), color = '#ff8700') +
geom_smooth(aes(x = Date, y = AKP), color = '#ff8700') +
geom_point(aes(x = Date, y = CHP), color = '#ff0000') +
geom_smooth(aes(x = Date, y = CHP), color = '#ff0000') +
geom_point(aes(x = Date, y = MHP), color = '#ff0019') +
geom_smooth(aes(x = Date, y = MHP), color = '#ff0019', se = FALSE) +
geom_point(aes(x = Date, y = HDP), color = '#8000ff') +
geom_smooth(aes(x = Date, y = HDP), color = '#8000ff', se = FALSE) +
geom_point(aes(x = Date, y = IYI), color = '#3db5e6') +
geom_smooth(aes(x = Date, y = IYI), color = '#3db5e6', se = FALSE) +
geom_point(aes(x = Date, y = SP), color = '#f50002') +
geom_smooth(aes(x = Date, y = SP), color = '#f50002', se = FALSE) +
geom_point(aes(x = Date, y = DEVA), color = '#0061a1') +
geom_smooth(aes(x = Date, y = DEVA), color = '#0061a1', se = FALSE) +
geom_point(aes(x = Date, y = GP), color = '#00564a') +
geom_smooth(aes(x = Date, y = GP), color = '#00564a', se = FALSE) +
scale_x_date(date_breaks = "3 months", date_labels = "%m/%y") +
labs(x = "", y = "") +
theme(axis.text.x = element_text(size = 20),
axis.text.y = element_text(size = 20))
I searched for answers but couldn't make any one of them work. How can I add a legend to the plot?

Related

change starting value for geom_bar

I have a plot that includes data from two different scales. So far, I've plotted both variables and adjusted the scale of one variable (ss) so that it is closer to the other variables. This greatly reduced the white space in the middle of the plot.
set.seed = 42
df <- data.frame(
cat = runif(10, 1, 20),
mean = runif(10, 350, 450),
ss = runif(10, 1, 50))
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(sec.axis = sec_axis(trans = ~.-250,
name = "sample size")) +
labs(y = "mean") +
theme_bw()
However, I don't love the really long bars for sample size, and I'd like to change the limits on the left y axis so that it starts 250 (where ss = 0). Unfortunately, if I replace my current scale_y_continuous parameter with limits (see below), then the bars disappear. How do I do this?
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(limits = c(250, 510), ### NEW Y AXIS LIMITS
sec.axis = sec_axis(trans = ~.-250,
name = "sample size")) +
labs(y = "mean") +
theme_bw()
EDIT: Updated plot with #AllanCameron's suggestion. This is really close, but it has the values of the bars extend below 0 on the secondary axis.
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(sec.axis = sec_axis(trans = ~.-250,
name = "sample size")) +
labs(y = "mean") +
theme_bw() +
coord_cartesian(ylim = c(250, 510)) ### NEW
Just expand parameter in scale_y_continuous() to c(0,0).
This tells ggplot2 to not add padding to the plot box.
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(sec.axis = sec_axis(trans = ~.-250, name = "sample size"),
expand = c(0,0)) + # New line here!
labs(y = "mean") +
theme_bw() +
coord_cartesian(ylim = c(250, 510))

Secondary axis in ggplot [duplicate]

I have the following tibble format and i want to create a chart with two y-axis.
sample <- climate <- tibble(
Month = c("1/1/2019","2/1/2019","3/1/2019","4/1/2019","5/1/2019","6/1/2019","7/1/2019","8/1/2019","9/1/2019","10/1/2019","11/1/2019","12/1/2019","1/1/2020","2/1/2020","3/1/2020"),
Reactions = c(52111,37324,212695,152331,24973,10878,7413,8077,13066,50486,8087,12600,31625,25578,20069),
Ratio = c(1371,1866,6445,4914,925,363,218,245,335,1530,352,525,1506,1112,873)
)
Here's what i tried so far.
ggplot() +
geom_bar(mapping = aes(x = sample$Month, y = sample$Reactions), stat = 'identity') +
geom_line(mapping = aes(x = sample$Month , y = sample$Ratio), size = 2, color = "red") +
scale_y_continuous(name = "Reactions per Month", sec.axis = sec_axis(trans = ~./20, name = "Reactions/ post"))
Any help will be appreciated
you have to recode Month column as date, and multiply Ratio times 20 (since you devided second axis by 20):
library(lubridate)
sample$Month <- mdy(sample$Month)
ggplot() +
geom_bar(mapping = aes(x = sample$Month, y = sample$Reactions), stat = 'identity') +
geom_line(mapping = aes(x = sample$Month , y = sample$Ratio*20), size = 2, color = "red") +
scale_y_continuous(name = "Reactions per Month", sec.axis = sec_axis(trans = ~./20, name = "Reactions/ post"))
you can also improve your code with use of data variable inside ggplot()
ggplot(sample, aes(x = Month)) +
geom_bar(aes(y = Reactions), stat = 'identity') +
geom_line(aes(y = Ratio*20), size = 2, color = "red") +
scale_y_continuous(name = "Reactions per Month", sec.axis = sec_axis(trans = ~./20, name = "Reactions/ post"))
Plot:

Use a gradient color fill for a bubble grid chart

I've got a bubble grid chart created but I can't for the life of my change the colors of the fill. I want to use a rainbow gradient based on the values. Below is my code and I've attached image out my output
setwd("C:/Users/Schelly/Desktop/Projects/Jens_tables_and_figures_2020/Bubble_chart")
library(tidyverse)
library(reshape2)
pc <- read.csv("Para_Bubble_data2.csv", header = TRUE)
head(pc)
pcm<-melt(pc, id = c("Sample"))
pcm$Sample <- factor(pcm$Sample,levels=unique(pcm$Sample))
xx = ggplot(pcm, aes(x = Sample, y = variable)) +
geom_point(aes(size = value, fill = value), alpha = 0.75, shape = 21) +
scale_colour_gradientn(colours=rainbow(4))+
scale_size_continuous(limits = c(0.000001, 1), range = c(1,17), breaks = c(.01,.10,.50,.75)) +
labs( x= "", y = "", size = "Relative Abundance (%)", fill = "")
xx
Output of code
You need to specify aes(colour = value) if you want to use scale_color_gradientn:
library(ggplot2)
df <- data.frame(x = factor(rep(1:5, each = 6)),
y = factor(rep(1:6, 5)), val = sample(30))
ggplot(df, aes(x = x, y = y, size = val, colour = val)) +
geom_point() +
scale_color_gradientn(colours = c("red", "yellow", "blue"))
If you want to use fill (to preserve a different outline colour), you need to use scale_fill_gradientn:
ggplot(df, aes(x = x, y = y, size = val)) +
geom_point(aes(size = val, fill = val), alpha = 0.75, shape = 21) +
scale_fill_gradientn(colours = rainbow(4))+
labs( x= "", y = "", size = "Relative Abundance (%)", fill = "")

barplot with lineplot - secondary axis

After referring to multiple links i have got to the below code however i still am not succeeding to get the line with labels. I suspect some mistake in sec.axis transformation but i can't figure it out.
# dummy data
df_dummy = data.frame('Plan_code'=c('A','B','C','D','E','F','G'),
'Total'=c(191432,180241,99164,58443,56616,29579,19510),'STP'=c(41,40,44,37,37,37,45))
# creation of plot
[![g <- ggplot(data = df_dummy, aes(x = Plan_code, y = Total)) +
geom_col(aes(fill = 'Total')) +
geom_line(data = df_dummy, aes(x = Plan_code, y = STP,group=1)) +
geom_point(data = df_dummy, aes(x = Plan_code,y=STP)) +
geom_label(data = df_dummy, aes(x = Plan_code, y = STP, fill = Plan_code, label = paste0('%', STP)), color = 'white', vjust = 1.6, size = 3) +
scale_y_continuous(sec.axis = sec_axis(~. / 2000, name = 'PERCENT')) +
labs(fill = NULL, color = NULL) +
theme_minimal()
print(g)][1]][1]
Like that?
g <- ggplot(data = df_dummy, aes(x = Plan_code, y = Total)) +
geom_col(aes(fill = 'Total')) +
geom_point(data = df_dummy, aes(x = Plan_code,y=STP * 2000)) +
geom_label(data = df_dummy, aes(x = Plan_code, y = STP *2000, fill = Plan_code, label = paste0('%', STP)), color = 'white', vjust = 1.6, size = 3) +
scale_y_continuous(sec.axis = sec_axis(~. / 2000, name = 'PERCENT'))+
geom_line(data = df_dummy, aes(x = Plan_code, y = STP * 2000,group=1), col = 'blue') +
theme(axis.text.y.right = element_text(color = 'blue'),axis.title.y.right = element_text(color = 'blue'))
labs(fill = NULL, color = NULL) +
theme_minimal()
I just multiplied your data with 2000, so that the absolute y-coordinates were right.
And I changed the color.

ggplot() geom_smooth() color gives me the wrong colors

I'm not sure why I'm getting my colors backward. Can someone please explain how the colors are assigned in this code? I'm trying to use examples on ggplot2 reference site, but I've been stuck on this for a long time. Here's the code:
#libraries
library(ggplot2)
library(scales)
library(reshape2)
#data
client.data <- read.csv('client.total.sorted.csv', header = TRUE, sep = ",")
#plot
ggplot(data = client.sorted) +
geom_smooth(mapping = aes(x = Date, y = Cancelled, color = "red")) +
geom_point(mapping = aes(x = Date, y = Cancelled, color = "red")) +
geom_smooth(mapping = aes(x = Date, y = Active, color = "green")) +
geom_point(mapping = aes(x = Date, y = Active, color = "green")) +
labs(title = "activations vs cancellations", x = "Date", y = "")
Here's the output:
I found this post referencing legends that helped me solve this:
Add legend to ggplot2 line plot
The solution that worked for me is this:
ggplot(data = client.sorted) +
geom_smooth(mapping = aes(x = Date, y = Cancelled, color = "CancelledLines")) +
geom_point(mapping = aes(x = Date, y = Cancelled, color = "CancelledPoints")) +
geom_smooth(mapping = aes(x = Date, y = Active, color = "greenLines")) +
geom_point(mapping = aes(x = Date, y = Active, color = "greenPoints")) +
scale_color_manual("",
breaks = c("CancelledLines", "CancelledPoints", "greenLines", "greenPoints"),
values = c("red", "red", "green", "green")) +
labs(title = "activations vs cancellations", x = "Date", y = "")

Resources