showing scale_fill_manual from scratch - r

I want to show histograms of multiple groups where the values do not stack. I do this by:
dat <- data.frame(x = seq(-3, 3, length = 20))
dat$y <- dnorm(dat$x)
dat$z <- dnorm(dat$x, mean = 2)
p <- ggplot(dat, aes(x = x)) +
geom_bar(aes(y = y), stat = "identity", alpha = .5, fill = "red") +
geom_bar(aes(y = z), stat = "identity", alpha = .5, fill = "blue")
I'd like to have a fill legend that shows the groupings. I'm not sure why this does not produce any legend (or error):
p + scale_fill_manual(values = c(x = "red", z = "blue"),
limits = c("mean 0", "mean 2")) +
guides(fill=guide_legend(title.position="top"))
Using unnamed values produces the same result.
Thanks,
Max

The legend is automatically generated only if you map fill to variable using aes, like so:
library(reshape2)
ggplot(melt(dat, "x"), aes(x = x)) +
geom_bar(aes(y = value, fill = variable),
stat = "identity", position = "identity", alpha = .5) +
scale_fill_manual(values = c(y = "red", z = "blue"),
labels = c("mean 0", "mean 2"))

Related

one of my box plots is being split into 2?

Call data
mydata = JRdata
Make ggplot/ reorder based on medians
p <- ggplot(mydata, aes(x= reorder((as.factor(female)),occlusion20, FUN = median, na.rm = TRUE), y = occlusion20, fill = FamilySelectionMetagenomics,
colour = FamilySelectionMetagenomics)) + geom_boxplot() +
scale_fill_manual(values = c("white","red")) +
scale_colour_manual(values = c("black", "black"))

Adding legend to bar chart with data from two data frames

I have two plots I just want to know how I can add a legend for the blue and gray bar charts and also could you please show me how you could also edit the legend tittle.
X1 <- c(seq(7.912087912,44.83516484,1.538461538))
X2 <- c(seq(7.912087912,49.45054945,1.538461538))
dat2 <- data.frame(x = X2 , y = rnorm(28, 26, 5))
dat1 <- data.frame(x = X1 , y = rnorm(100, 25, 4))
ggplot(NULL) +
geom_bar(dat1, mapping = aes(x = x, y = y), stat = "identity",alpha = 0.3, position = "stack" ) + labs( x = " Time [ S ]", y = "Frequency") + theme_minimal() +
ggtitle("Histogram Of Time In Tank") + theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(hjust = 0.5)) +
geom_bar(dat2, mapping = aes(x = x, y = y ), stat = "identity", alpha = .3, position = "stack", fill='lightblue' , color='lightblue4')
+ scale_linetype_discrete(name = LegendTitle)
If you want a legend in ggplot, you need to have an aesthetic mapping inside your aes() or no legend will appear. Here's how we can set a mapping and then use the scale to set the colors we want
ggplot(NULL) +
geom_bar(dat1, mapping = aes(x = x, y = y, fill="Grey Bars"), stat = "identity",alpha = 0.3, position = "stack" ) +
labs( x = " Time [ S ]", y = "Frequency") +
theme_minimal() +
ggtitle("Histogram Of Time In Tank") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_bar(dat2, mapping = aes(x = x, y = y, fill='Blue Bars') , stat = "identity", alpha = .3, position = "stack", color='lightblue4') +
scale_fill_manual(name="Bars", values=c("Grey Bars" = "grey35", "Blue Bars" = "lightblue"))

Defining Legends using ggplot

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

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

R ggplot placing labels on error bars

I can create a bar chart with error bars but how can I put labels on the higher (vLABELH) and lower error bars (vLABELL).
library(ggplot2)
vx <- c(1:5)
vBAR <- c(0.1,0.2,0.3,0.4,0.5)
vLINE1 <- c(0.15,0.25,0.35,0.45,0.55)
vLINE2 <- c(0.15,0.25,0.35,0.45,0.55)
vLINE3 <- c(0.05,0.15,0.25,0.35,0.45)
vLABELL<- c(0.05,0.15,0.25,0.35,0.45)
vLABELH <- c(0.15,0.25,0.35,0.45,0.55)
df1 <- as.data.frame(cbind(vx,vBAR,vLINE1,vLINE2,vLINE3,vLABELL,vLABELH))
class(df1)
barchart1 <- ggplot(df1, aes(x=as.factor(vx),y=vBAR)) + geom_bar(fill="blue", colour="blue")+
geom_errorbar(aes(ymin=vLINE3, ymax=vLINE1 ))
barchart1
I suppose you're looking for geom_text.
ggplot(df1, aes(x = as.factor(vx), y = vBAR)) +
geom_bar(stat = "identity", fill = "blue", colour = "blue") +
geom_errorbar(aes(ymin = vLINE3, ymax = vLINE1 )) +
geom_text(aes(label = vLABELH, y = vLINE1), vjust = -.5) +
geom_text(aes(label = vLABELL, y = vLINE3), vjust = 1.5)

Resources