color key legend in r - r

I am trying hard to figure out to add color gridient label to my plot ( link to previous question). Sorry for keep asking but this is maximum I could push me forward.
#data 1:
lab1 <- 1:10
group <- rep(1:3, each = length (lab1))
label <- rep(lab1, 3)
avar <- rep(c(0, 1, 4, 5, 6, 8, 10, 11, 12, 13), 3)
myd <- data.frame (group, label, avar)
# data 2
fillcol <- rep(rnorm(length(lab1)-1, 0.5, 0.2), 3)
group1 <- rep(1:3, each = length(fillcol)/3)
# this variable will be used to fill color in bars
filld <- data.frame(group1, fillcol)
colbarplot <- function(group) {
myd1 <- myd[myd$group == group,]
filld1 <- filld[filld$group1 == group,]
blues <- colorRampPalette(c("yellow", "blue"))
barplot(as.matrix(diff(myd1$avar)), horiz=T,
col=blues(10)[10* filld1$fillcol],
axes=F, xlab="Mark")
axis(1, labels=myd$label, at=myd$avar)
axis(3, labels=myd$avar, at=myd$avar)
}
par(mfrow = c(4, 1))
par(mar = c(2.5, 1, 2.5, 1))
sapply(unique(myd$group),function(x) colbarplot(x))
Now I am struggling to add legend, sorry for this new user.
blues <- colorRampPalette(c("yellow", "blue"))
colors <- blues(10)
count <- length(colors)
m <- matrix(1:count, count, 1)
m1 <- m
image(m, col=colors, ylab="", axes=FALSE)
I produced color scale that is not what I am expecting, I am trying plot a smaller legend, less in width and height, along with original scale use in color coding.
Here are some unsuccessful trials for labeling:
colab <- c(round (min(filld$fillcol), 2), round(max(filld$fillcol), 2))
colpos <- c(0.33 * max(mapdat$position),0.66 * max(mapdat$position))
axis(1, labels=colab, at=colpos)

Getting a decent legend is much easier with ggplot2
library(plyr)
myd$group <- factor(myd$group)
gData <- ddply(myd, .(group), function(x){
data.frame(delta = diff(x$avar), label = paste(head(x$label, -1), tail(x$label, -1), sep = "-"))
})
gData$FillCol <- rnorm(nrow(gData))
ggplot(gData, aes(x = group, y = delta, fill = FillCol, label = label)) + geom_bar(stat = "identity") + coord_flip() + scale_fill_gradient(low = "blue", high = "yellow") + geom_text(position = "stack")

Related

Set / Link point and shape options for variables in ggplot2

I would like to link variables I have in a dataframe i.e. ('prop1', 'prop2', 'prop3') to specific colours and shapes in the plot. However, I also want to exclude data (using dplyr::filter) to customise the plot display WITHOUT changing the points and shapes used for a specific variable. A minimal example is given below.
library(ggplot2)
library(dplyr)
library(magrittr)
obj <- c("cmpd 1","cmpd 1","cmpd 1","cmpd 2","cmpd 2")
x <- c(1, 2, 4, 7, 3)
var <- c("prop1","prop2","prop3","prop2","prop3")
y <- c(1, 2, 3, 2.5, 4)
col <- c("#E69F00","#9E0142","#56B4E9","#9E0142","#56B4E9")
shp <- c(0,1,2,1,2)
df2 <- cbind.data.frame(obj,x,var,y,col,shp)
plot <- ggplot(data = df2 %>%
filter(obj %in% c(
"cmpd 1",
"cmpd 2"
)),
aes(x = x,
y = y,
colour = as.factor(var),
shape = as.factor(var))) +
geom_point(size=2) +
#scale_shape_manual(values=shp) +
#scale_color_manual(values=col) +
facet_grid(.~obj)
plot
However, when I redact cmpd1 (just hashing in code) the colour and shape of prop2 and prop3 for cmpd2 change (please see plot2).
To this end, I tried adding in scale_shape_manual and scale_color_manual to the code (currently hashed) and linked these to specific vars (col and shp) in the dataframe (df2), but the same problem arises that both the shape and color of these variables changes when excluding one of the conditions?
Any and all help appreciated.
Try something like this:
library(tidyverse)
obj <- c("cmpd 1","cmpd 1","cmpd 1","cmpd 2","cmpd 2")
x <- c(1, 2, 4, 7, 3)
var <- c("prop1","prop2","prop3","prop2","prop3")
y <- c(1, 2, 3, 2.5, 4)
df2 <- cbind.data.frame(obj,x,var,y)
col <- c("prop1" = "#E69F00",
"prop2" = "#9E0142",
"prop3" = "#56B4E9")
shp <- c("prop1" = 0,
"prop2" = 1,
"prop3" = 2)
plot <- ggplot(data = df2 %>%
filter(obj %in% c(
"cmpd 1",
"cmpd 2"
)),
aes(x = x,
y = y,
colour = var,
shape = var)) +
geom_point(size=2) +
scale_shape_manual(values=shp) +
scale_color_manual(values=col) +
facet_grid(.~obj)
plot

barplot() with data points - base R

I'm trying to plot bar plot with data points on top in base R.
I'm using base R because it's impossible to create in a simple way texture fill in ggplot (e.g. see here, and ggtexture doesn't allow complex editing).
Using barplot() function and points(), I can do this:
library(tidyverse)
#Sample data
data <- iris %>%
group_by(Species) %>%
summarise(m = mean(Sepal.Length),
se = sd(Sepal.Length)/
sqrt(sum(!is.na(Sepal.Length)))) %>%
ungroup()
chart <- barplot(height=data$m, names=data$Species,
density = c(5, 5, 5),
angle = c(0,45,90),
col = "brown",
width = c(0.1,0.1,0.1),
font.axis = 2,
border = c("black"),
lwd = 2)
points(x = chart,
y = data$m)
However, I would like to create something similar to the below:
iris %>%
group_by(Species) %>%
summarise(m = mean(Sepal.Length),
se = sd(Sepal.Length)/
sqrt(sum(!is.na(Sepal.Length)))) %>%
ungroup() %>%
ggplot(aes(Species, m,
group = Species,
color = Species,
shape = Species)) +
geom_bar(stat = "identity", fill="white",
color="black") +
geom_jitter(
aes(Species, Sepal.Length),
data = iris)
Converting Species to factor using barplot output as labels. When then converted back to numeric using the as.numeric(as.character(x))) approach, the points appear at the right places for each group.
# op <- par(xpd=TRUE)
b <- barplot(with(iris, tapply(Sepal.Length, Species, mean)), density=c(5, 5, 5),
angle=c(0, 45, 90),
col="brown",
width=c(0.1, 0.1, 0.1),
font.axis=2,
border=c("black"),
lwd=2,
ylim=c(0, max(jitter(iris$Sepal.Length)) * 1.15) ## better use dynamic ylim
)
iris$Species.f <- factor(iris$Species, labels=b)
with(iris, points(jitter(as.numeric(as.character(iris$Species.f))),
jitter(Sepal.Length), pch=as.numeric(Species) + 14,
col=as.numeric(Species) + 1, cex=.8))
legend("topleft", title="Species", legend=levels(iris$Species),
pch=seq(levels(iris$Species)) + 14, col=seq(levels(iris$Species)) + 1,
horiz=TRUE, cex=.8)
box(lwd=2)
# par(op)
You can use the jitter function here.
chart <- barplot(height=data$m, names=data$Species,
density = c(5, 5, 5),
angle = c(0,45,90),
col = "brown",
width = c(0.1,0.1,0.1),
font.axis = 2,
border = c("black"),
lwd = 2, las=1,
ylim=c(0,8))
points(x = lapply(rep(chart, each=50), jitter, amount=0.05),
y = iris$Sepal.Length,
col=iris$Species, pch=20)

Colouring of datapoint with ggplot2 and RColorBrewer

I have got a df with over 400 datapoints and want to colour those accourding to the RColorBrewer package with the palette = "Blues".
I plotted my data and even expanded the color input maximum of the palette to the length of my data points to avoid getting "Error messages" (see below), but the colors in the plot aren't changing (only a black line).
Error: Aesthetics must be either length 1 or the
same as the data (427): fill
I've created a dummy df to make my problem reproducible:
library(ggplot2)
library(RColorBrewer)
color = colorRampPalette(rev(brewer.pal(n = 9, name = "Blues")))(300)
df = (curve(3*x^2 + x, from=1, to=10, n=300, xlab="xvalue", ylab="yvalue",
col="blue", lwd=2, main="Plot of (3x^2 + x)"))
dfx = matrix(data = df$x, ncol = 1)
dfy = matrix(data = df$y, ncol = 1)
dfa = cbind(dfx,dfy)
DF = ggplot(dfa, aes(x = dfx, y = dfy)) +
geom_point(fill = color)
I expect the curve to change into a light blue at the start (1,4) with an increase in darkness till the end (dark blue at the end (10,310)).
Thanks in advance!
color_seq = colorRampPalette(brewer.pal(n = 9, name = "Blues"))(300)
df = (curve(3*x^2 + x, from=1, to=10, n=300, xlab="xvalue", ylab="yvalue",
col="blue", lwd=2, main="Plot of (3x^2 + x)"))
dfx = matrix(data = df$x, ncol = 1)
dfy = matrix(data = df$y, ncol = 1)
dfa = as.data.frame(cbind(dfx , dfy, color_seq), stringsAsFactors = FALSE)
dfa$V1 <- as.numeric(dfa$V1) ## convert both to numeric so the scale is continous
dfa$V2 <- as.numeric(dfa$V2)
ggplot(dfa, aes(x = V1, y = V2, color = color_seq)) +
geom_point() +
scale_color_identity()
exDF <- data.frame(dataX = seq(1, 10, .1),
dataY = sapply(seq(1, 10, .1), function(x) 3*x^2 + x))
exDFcolors <- colorRampPalette(brewer.pal(9, "Blues"))(nrow(exDF))
ggplot(exDF, aes(dataX, dataY)) +
geom_line(size = 2, color = exDFcolors)

How to add two different magnitudes of point size in a ggplot bubbles chart?

I just encountered such graph attached where two colors of geom_point are used (I believe it is made by ggplot2). Similarly, I would like to have dots of one color to range from size 1 to 5, and have another color for a series of dots for the range 10 to 50. I have however no clue on how to add two different ranges of point in one graph.
At the basic step I have:
a <- c(1,2,3,4,5)
b <- c(10,20,30,40,50)
Species <- factor(c("Species1","Species2","Species3","Species4","Species5"))
bubba <- data.frame(Sample1=a,Sample2=b,Species=Species)
bubba$Species=factor(bubba$Species, levels=bubba$Species)
xm=melt(bubba,id.vars = "Species", variable.name="Samples", value.name = "Size")
str(xm)
ggplot(xm,aes(x= Samples,y= fct_rev(Species)))+geom_point(aes(size=Size))+scale_size(range = range(xm$Size))+theme_bw()
Any would have clues where I should look into ? Thanks!
I've got an approach that gets 90% of the way there, but I'm not sure how to finish the deed. To get a single legend for size, I used a transformation to convert input size to display size. That makes the legend appearance conform to the display. What I don't have figured out yet is how to apply a similar transformation to the fill so that both can be integrated into the same legend.
Here's the transformation, which in this case shrinks everything 10 or more:
library(scales)
shrink_10s_trans = trans_new("shrink_10s",
transform = function(y){
yt = if_else(y >= 10, y*0.1, y)
return(yt)
},
inverse = function(yt){
return(yt) # Not 1-to-1 function, picking one possibility
}
)
Then we can use this transformation on the size to selectively shink only the dots that are 10 or larger. This works out nicely for the legend, aside from integrating the fill encoding with the size encoding.
ggplot(xm,aes(x= Samples,y= fct_rev(Species), fill = Size < 10))+
geom_point(aes(size=Size), shape = 21)+
scale_size_area(trans = shrink_10s_trans, max_size = 10,
breaks = c(1,2,3,10,20,30,40),
labels = c(1,2,3,10,20,30,40)) +
scale_fill_manual(values = c(rgb(136,93,100, maxColorValue = 255),
rgb(236,160,172, maxColorValue = 255))) +
theme_bw()
a <- c(1, 2, 3, 4, 5)
b <- c(10, 20, 30, 40, 50)
Species <- factor(c("Species1", "Species2", "Species3", "Species4", "Species5"))
bubba <- data.frame(Sample1 = a, Sample2 = b, Species = Species)
bubba$Species <- factor(bubba$Species, levels = bubba$Species)
xm <- reshape2::melt(bubba, id.vars = "Species", variable.name = "Samples", value.name = "Size")
ggplot(xm, aes(x = Samples, y = fct_rev(Species))) +
geom_point(aes(size = Size, color = Size)) +
scale_color_continuous(breaks = c(1,2,3,10,20,30), guide = guide_legend()) +
scale_size(range = range(xm$Size), breaks = c(1,2,3,10,20,30)) +
theme_bw()
Here's a cludge. I haven't got time to figure out the legend at the moment. Note that 1 and 10 are the same size, but a different colour, as are 3 and 40.
# Create data frame
a <- c(1, 2, 3, 4, 5)
b <- c(10, 20, 30, 40, 50)
Species <- factor(c("Species1", "Species2", "Species3", "Species4", "Species5"))
bubba <- data.frame(Sample1 = a, Sample2 = b, Species = Species)
# Restructure data
xm <- reshape2::melt(bubba, id.vars = "Species", variable.name = "Samples", value.name = "Size")
# Calculate bubble size
bubble_size <- function(val){
ifelse(val > 3, (1/15) * val + (1/3), val)
}
# Calculate bubble colour
bubble_colour <- function(val){
ifelse(val > 3, "A", "B")
}
# Calculate bubble size and colour
xm %<>%
mutate(bub_size = bubble_size(Size),
bub_col = bubble_colour(Size))
# Plot data
ggplot(xm, aes(x = Samples, y = fct_rev(Species))) +
geom_point(aes(size = bub_size, fill = bub_col), shape = 21, colour = "black") +
theme(panel.grid.major = element_line(colour = alpha("gray", 0.5), linetype = "dashed"),
text = element_text(family = "serif"),
legend.position = "none") +
scale_size(range = c(1, 20)) +
scale_fill_manual(values = c("brown", "pink")) +
ylab("Species")
I think you are looking for bubble plots in R
https://www.r-graph-gallery.com/bubble-chart/
That said, you probably want to build the right and left the side of the graphic separately and then combine.

How to have background fill in the plot in R

So, I've a t-dist plot created in R using curve and adding on the polygons onto that. It gives me a basic looking plot.
What I need is a more good looking plot where
X-axis starts from -6
Y-axis starts from 0
Background of the plot(except under the curve) is filled with some color which I need
I think I need to use the ggplot2 package for this, so answers based on ggplot2 usage is what I need. Or any answer that would return me that output is appreciated.
Here is my code
curve(dt(x, df = 7), from = -6, to = 6)
x <- seq(-1.96, -6, len = 100)
y <- dt(x, 7)
x1 <- seq(1.96, 6, len = 100)
y1 <- dt(x1, 7)
polygon(c(x1[1], x1, x1[100]), c(dt(-6, 7), y1, dt(6, 7)),
col = "#b14025", border = "black")
polygon(c(x[1], x, x[100]), c(dt(-6, 7), y, dt(6, 7)),
col = "#b14025", border = "black")
First Image is the current Output
Second Image is what I think it should look like
Here is one way to obtain a similar result using the ggplot2 package:
library(ggplot2)
dt_tails <- function(x){
y <- dt(x,7)
y[abs(x) < 1.96] <- NA
return(y)
}
dt_7 <- function(x) dt(x,7)
p <- ggplot(data.frame(x=c(-6,6)),aes(x=x)) +
stat_function(fun=dt_7, geom="area", fill="white", colour="black")
p <- p + stat_function(fun=dt_tails, geom="area", fill='#b14025')
p <- p + theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_rect(fill="#eae9c8") )
plot(p)
Since you expected a ggplot answer, just add + theme(panel.background = element_rect(fill = "yellow")) to your plot or what ever color you like.
I finally managed to do it with the base plotting functions only.
For Shading the area outside curve: I just added one more polygon tracing the area outside the curve.
For fixing the graph to start at the required X and Y, I used another parameter of plot function xaxs & yaxs from this Link
Here is my attached code
curve(dt(x, df = 7), from = -6, to = 6,xaxs="i",yaxs="i",ylim=c(0,0.4))
t = seq(-6,6,len = 100)
yt = dt(t,7)
x <- seq(-1.96, -6, len = 100)
y <- dt(x, 7)
x1 <- seq(1.96, 6, len = 100)
y1 <- dt(x1, 7)
polygon(x = c(-6,-6,t,6,6),
y = c(0.4,0,yt,0,0.4),
col = "#eae9c8",
border = "black")
polygon(x = c(x1[1], x1, x1[100]),
y = c(dt(-6, 7), y1, dt(6, 7)),
col = "#b14025",
border = "black")
polygon(x = c(x[1], x, x[100]),
y = c(dt(-6, 7), y, dt(6, 7)),
col = "#b14025",
border = "black")
Here is the attached output

Resources