Defining Legends using ggplot - r

I am trying to define the legends of my plot in R.
I have the following code, this is a demo only, real data will have 7-8 columns and up to 20 samples
library(ggplot2)
library(RColorBrewer)
colors <-brewer.pal(n = 3, name = 'Paired')
ids <- c("TestA", "TestB", "TestC")
bg <-c(23, 13, 15)
sample1 <- c(21,15,17)
sample2 <- c(27,25,11)
sample3 <- c(24,14,18)
df <- data.frame(ids, bg, sample1,sample2,sample3)
ggplot(df) +
geom_col(aes(x = ids, y = bg), size = 1, color = "grey", fill = "grey") +
geom_point(aes(x = ids, y = sample1), size = 10, color=colors[1], group = 1) +
geom_point(aes(x = ids, y = sample2), size = 10, color=colors[2], group = 1) +
geom_point(aes(x = ids, y = sample3), size = 10, color=colors[3], group = 1)+
ggtitle("Plot title") +
xlab("x label") + ylab(" y label") +
scale_colour_manual(values = c("95% PI"= "black",
"Forecasts" = "red",
"Threshold" = "green"))
It produces the following output
How can I add legends like the following picture, and ensure that the color matches, i.e. legend color matches sample or bg color

Is this something like what you want?
ggplot(df) +
geom_col(aes(x = ids, y = bg, fill = "background"), size = 1, color = "grey") +
geom_point(data = df %>% pivot_longer(cols = starts_with("sample"), names_to = "sample", values_to = "values"), aes(x = ids, y = values, color = sample), size = 10) +
ggtitle("Plot title") +
xlab("x label") + ylab(" y label") +
scale_colour_manual(name = NULL, values = c("sample1"= "black",
"sample2" = "red",
"sample3" = "green"),
labels = c("95% PI", "forecasts", "Threshold")) +
scale_fill_manual(name = NULL, values = c("background" = "grey"))

Related

scale_color_manual is not working for 2 line plot with break in y-axis (ggplot2)

I've been trying for a while now and also doing a lot of research, but I just can't get it to add a simple legend for my two lines.
I have two lines in my chart and I just want to add a legend for the two lines.
scale_color_manual did not work. I suspect it is because I am using scale_y_break. In other plots (without scale_y_break) scale_color_manual works without problems.
Here is my code:
day <- c(1:5)
altimeter <- c(8.291, 8.872, 7.212, 8.1, 5.92)
slope_kilometers <- c(30.23, 34.8, 29.34, 32.98, 21.23)
df2 <- data.frame(day, altimeter, slope_kilometers)
library(ggbreak)
altimeter_color <- "steelblue"
slope_kilometers_color <- "darkred"
ggplot(df2, aes(x = day)) +
#Altimeter data
geom_line(aes(y = altimeter),
linetype = 2,
linewidth = 1,
color = altimeter_color) +
geom_point(y = altimeter, size = 3, color = altimeter_color) +
#Slope kilometers data
geom_line(aes(y = slope_kilometers),
linetype = 2,
linewidth = 1,
color = slope_kilometers_color) +
geom_point(y = slope_kilometers, size = 3, color = slope_kilometers_color) +
#Y-Axis
scale_y_break( c(9, 20), scales = 1.5) +
#Label
labs(x = "Tage",
y = "[km]") +
#Legend
scale_color_manual(values = c(altimeter_color, slope_kilometers_color)) +
#Title
ggtitle("Höhenmeter und Pistenkilometer meines 5-tägigen Skiurlaubs")
I tried different versions of scale_color_manual, labs, aes(fill="")
Update: I tweaked the former plot (removed):
One way to achieve what you want is:
First bring data in long format then put color inside aesthetics:
Rule of thumb: What is in aesthetics will have a legend:
library(tidyverse)
library(ggbreak)
df2 %>%
pivot_longer(-day) %>%
ggplot(aes(x = day)) +
#Altimeter data
geom_line(data = . %>% filter(name == "altimeter"), aes(y = altimeter, color = name),
linetype = 2, linewidth = 1 ) +
geom_point(data = . %>% filter(name == "altimeter"), aes(y = altimeter, color = name), size = 3) +
#Slope kilometers data
geom_line(data = . %>% filter(name == "slope_kilometers"), aes(y = slope_kilometers, color = name),
linetype = 2, linewidth = 1) +
geom_point(data = . %>% filter(name == "slope_kilometers"), aes(y = slope_kilometers, color = name), size = 3) +
#Y-Axis
scale_y_break( c(9, 20), scales = 1.5) +
#Label
labs(x = "Tage", y = "[km]", color = "") +
#Legend
scale_color_manual(values = c(altimeter_color, slope_kilometers_color)) +
#Title
ggtitle("Höhenmeter und Pistenkilometer meines 5-tägigen Skiurlaubs") +
theme(legend.position = "bottom")

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 = "")

Legend for additional points in a ggplot

Steeling the example of this question (Link), I want to ask if it is possible to add the additional blue point to the legend?
dat <- data.frame(cond = rep(c("A", "B"), each=10),
xvar = 1:20 + rnorm(20,sd=3),
yvar = 1:20 + rnorm(20,sd=3))
g1 <- dat[15,]
ggplot(dat, aes(x = xvar, y = yvar, shape = cond,
colour = cond), size = 2.5) +
geom_point(alpha = 1) +
geom_point(data = g1, colour = "blue", size = 4, show_guide = FALSE)
You can put the aesthetics for the additional points layer inside aes instead of outside to get it added to the legend. You can use any string value; that string will be the name in the legend.
Then you can control the color and shape of that point via scale_*_manual layers.
I additionally changed the size of that point in the legend using override.aes, which is optional.
ggplot(dat, aes(x = xvar, y = yvar, shape = cond,
colour = cond), size = 2.5) +
geom_point(alpha = 1) +
geom_point(data = g1, aes(colour = "Point 15", shape = "Point 15"), size = 4) +
scale_shape_manual(values = c(16, 17, 17) ) +
scale_color_manual(values = c("pink", "turquoise", "blue") ) +
guides(color = guide_legend( override.aes = list(size = c(1.5, 1.5, 4) ) ) )
You probably have to change the condition of that point in the data as in your example or add it to the date, if it is not already part of it.
dat <- data.frame(cond = rep(c("A", "B"), each=10),
xvar = 1:20 + rnorm(20,sd=3),
yvar = 1:20 + rnorm(20,sd=3))
dat$size = 2.5
dat[15,]$cond = "C"
dat$cond = as.character(dat$cond)
dat[15,]$size = 4
ggplot(dat, aes(x = xvar, y = yvar, shape = cond,
colour = cond, size=size)) +
geom_point(alpha = 1) +
scale_colour_manual(values=c("red", "turquoise", "blue")) +
scale_size_continuous(guide = FALSE)

Labeling a point by giving its x and y axes on geom_histogram

How to show the point (x=0, y=1500) with a text label next to it on the following histogram?
ggplot(ds_visits, aes(x = patientsInService)) +
geom_histogram(stat = "count", col = "black", fill = "white") +
theme_bw() +
labs(x = "Patients in service", y = "Cases") +
scale_x_discrete(limits = seq(0, 5, 1))
You have to create dummy data.frame for point data:
pointData <- data.frame(X = 0, Y = 1500)
Plot it with with two additional gems (geom_point and geom_text):
ggplot(ds_visits, aes(patientsInService)) +
geom_histogram(stat = "count", col = "black", fill = "white") +
geom_point(data = pointData, aes(X , Y)) +
geom_text(data = pointData, aes(X + 1 , Y + 10, label = "My Text"))
In geom_text I'm changing coordinates a little bit not to overlap text with point.

Legend geom_hline not in right order

I've made a barplot in ggplot, and added a couple of lines. What happens is that the color and description of the lines don't correspond:
The yellow line should have the description 'Median Member', but is displayed as 'avg Member'. What happens here? The code I used:
library(ggplot2)
library(dplyr)
MemberID=c(1,1,1, 2, 2, 2)
ClientCode = c(10,100,1000, 20, 200, 2000)
Duration = c(2356, 1560, 9000, 4569, 3123, 8000)
df <- data.frame(MemberID, ClientCode, Duration)
dr <- df %>%
filter(MemberID == 1)
dr_avg <- df
ggplot(dr, aes(reorder(as.character(ClientCode), -Duration), Duration, fill=-Duration)) +
geom_bar(stat="identity") + # the height of the bar will represent the value in a column of the data frame
xlab('ClientCode') +
ylab('Duration (Minutes)') +
geom_hline(data=dr, aes(yintercept=mean(Duration), linetype = 'Avg Member'), color = 'red', show.legend = TRUE) +
geom_hline(data=dr, aes(yintercept=median(Duration), linetype = 'Median Member'), color = 'orange', show.legend = TRUE) +
geom_hline(data=dr_avg, aes(yintercept=mean(Duration), linetype = 'Avg all data'), color = 'blue', show.legend = TRUE) +
scale_linetype_manual(name = "Line", values = c(2, 2, 2), guide = guide_legend(override.aes = list(color = c("red", "orange", "blue")))) +coord_flip()
Don't create geom_hline for every line you want to insert. What if you have hundred of them? Create a separate object d and specify different linetypes and colors there geom_hline(data = d, aes(yintercept = value, linetype = name, color = name)). When you want to specify colors use: scale_colour_manual(values = c("red", "orange", "blue")).
d1 <- summarize(df, mean(Duration), median(Duration))
d2 <- summarize(dr_avg, mean(Duration))
d <- data.frame(value = as.numeric(c(d1, d2)),
name = c('Avg Member', 'Median Member', 'Avg all data'))
ggplot(dr, aes(reorder(as.character(ClientCode), -Duration),
Duration,
fill = factor(-Duration))) +
geom_bar(stat = "identity") +
labs(x = "ClientCode",
y = "Duration (Minutes)") +
geom_hline(data = d, aes(yintercept = value, linetype = name, color = name)) +
scale_fill_brewer(palette = "Dark2") +
scale_colour_manual(values = c("red", "orange", "blue")) +
coord_flip() +
theme_bw()
PS.: Data that you provided doesn't make sense as two lines overlap.

Resources