I have made a cumulative incidence plot using cmprsk::cuminc and then ggcompetingrisks for plotting.
I want to change legend labels for the group.
I've tried with legend.lab = c("A", "B", "C").
This works when I do a ggsurvplot without competing risk. But it does not work now.
Any suggestions?
My code is like:
fit <- cuminc(df$Time, df$Event, group = df$genotype)
p <- ggcompetingrisks(fit, multiple_panels = FALSE, palette = "black",
legend.title = c("genotype"),
legend.labs = c("A", "B", "C"))
The thing is that the legend show both the Event (0, 1) and the Group (1, 2, 3). I want only the group to be shown in the legend and I want it to be named A, B and C..
Please help me!!
If I get your fit correct, group will be plotted as linetype so you can use scale_linetype_manual() to set the title of the legend, and the names. To turn of the other legend about event, you can use guides()
See below for an example using some simulated data:
library(survminer)
library(ggplot2)
library(cmprsk)
df = data.frame(
Time = rexp(100),
genotype = factor(sample(1:3,100,replace=TRUE)),
Event = factor(sample(0:1,100,replace=TRUE),0:1,c('no event', 'death'))
)
fit <- cuminc(df$Time, df$Event, group = df$genotype)
ggcompetingrisks(fit)
p <- ggcompetingrisks(fit, multiple_panels = FALSE)
p + scale_linetype_manual(name="genotype",values=1:3,labels=c("A","B","C"))+
guides(col="none")
Please do dput(df) and paste the output as part of your question if the df I have above is different from yours. This way others can help as well.
If you need thicker lines, do:
old_geom <- ggplot2:::check_subclass("line", "Geom")$default_aes
update_geom_defaults("line", list(size = 1.5))
p + scale_linetype_manual(name="genotype",values=1:3,labels=c("A","B","C"))+
guides(col="none")
update_geom_defaults("line", list(size = old_geom$size))
Related
I´m new to R. I cannot put a legend in a plot chart, there is someting wrong with my code, I´ve tried to put a legend on this plot. I did a hash to put some colors on my model, but now I can´t figure out how to put the correct legend on it. This is what I di:
myhash<-c(a="green",b="pink",c="blue",d="purple",e="orange",f="brown",g="yellow",
h="black",i="gray")
mycolor<-myhash[df$category]
plot(df$growth,df$tannin,col=mycolor,cex=1,pch=16,xlab="Crecimiento",ylab="Taninos", main = "Modelo lineal Taninos vs Crecimiento")
I tried to put the legend like this :
legend("topright",c(df$tannin),fill=c("green","pink","blue","purple","orange","brown","yellow",
"black","gray"))
Please let me know how can I fix it, Im very beginner in R. Also, I cannot use any library.
Thanks in advance.
The problem seems to be that there is no legend (the argument to function legend, not the function) to show as text beside the fill colors.
To use a named argument, legend = unique(names(mycolor)) solves the problem.
set.seed(2020)
df <- data.frame(category = rep(c("a","b", "c", "d"), 4),
growth = 1:16, tannin = cumsum(rnorm(16)))
myhash <- c(a = "green", b = "pink", c = "blue", d = "purple")
mycolor <- myhash[df$category]
plot(df$growth, df$tannin, col = mycolor, cex = 1, pch = 16,
xlab = "Crecimiento", ylab = "Taninos",
main = "Modelo lineal Taninos vs Crecimiento")
legend("topright", legend = unique(names(mycolor)), fill = mycolor)
I need to map my Erosion values for different levels of tillage (colomns) with three levels of soil depth (rows (A1, A2, A3)). I want all of this to be shown as a barchart in a single plot.
Here is a reproducible example:
a<- matrix(c(1:36), byrow = T, ncol = 4)
rownames(a)<-(c("A1","B1","C1","A2","B2","C2","A3","B3","C3"))
colnames(a)<-(c("Int_till", "Redu_till", "mulch_till", "no_till"))
barplot(a[1,], xlab = "A1", ylab = "Erosion")
barplot(a[4,], xlab = "A2", ylab = "Erosion")
barplot(a[7,], xlab = "A3", ylab = "Erosion")
##I want these three barchart side by side in a single plot
## for comparison
### and need similar plots for all the "Bs" and "Cs"
### Lastly, i want these three plots in the same page.
I have seen people do similar things using "fill" in ggplot (for lines) and specifying the factor which nicely separates the chart for different categories but I tried doing it but always run into error maybe because my data is continuous.
If any body could help me with these two things.. It will be a great help. I will appreciate it.
Thank you!
We can use ggplot
library(reshape2)
library(ggplot2)
library(dplyr)
melt(a) %>%
ggplot(., aes(x = Var2, y = value, fill = Var1)) +
geom_bar(stat = 'identity',
position = position_dodge2(preserve = "single")) +
facet_wrap(~ Var1)
Set mfcol to specify a 3x3 grid and then for each row generate its bar plot. Also, you could consider adding the barplot argument ylim = c(0, max(a)) so that all graphs use the same Y axis. title and mtext can be used to set the overall title and various margin text as we do below. See ?par, ?title and ?mtext for more information.
opar <- par(mfcol = c(3, 3), oma = c(0, 3, 0, 0))
for(r in rownames(a)) barplot(a[r, ], xlab = r, ylab = "Erosion")
par(opar)
title("My Plots", outer = TRUE, line = -1)
mtext(LETTERS[1:3], side = 2, outer = TRUE, line = -1,
at = c(0.85, 0.5, 0.17), las = 2)
I have plotted a stacked barplot in base R:
set.seed(12)
dat <- matrix(sample(1:6, 6) , nrow = 3)
rownames(dat) <- c("A", "B", "C")
colnames(dat) <- c("blue", "green")
barplot(dat, beside = TRUE)
I'd like to
assign one color to each group of bars and
change the lightness (black to color) depending on bar length, so that a bar of length 6 has the full color.
like this:
How can I achieve this?
I have adapted #nya's answer in the following way:
set.seed(12)
dat <- matrix(sample(1:6, 6) , nrow = 3)
rownames(dat) <- c("A", "B", "C")
colnames(dat) <- c("blue", "green")
sequence <- seq(1, 6, 1) # min = 1, max = 6, steps = 1
resolution <- length(sequence)
blues <- colorRampPalette(c("black", "blue"))(resolution)
greens <- colorRampPalette(c("black", "green"))(resolution)
cols <- c(blues[match(dat[, "blue"], sequence)],
greens[match(dat[, "green"], sequence)])
mar.default <- c(5,4,4,2) + 0.1
par(mar = mar.default + c(2, 0, -2, 0))
barplot(dat, beside = TRUE, col = cols)
First, we need to create color gradients for plotting. Let's use a resolution of 10 colors from black to either blue or green.
res <- 10
blues <- colorRampPalette(c("black", "blue"))(res)
greens <- colorRampPalette(c("black", "green"))(res)
Second, we will use the function cut coerced to a factor to determine where in the gradient the values fit. We will use the factors (interpreted as numbers in R when subsetting rows) to select, which colors correspond to which values in data.
b.cut <- as.factor(cut(dat[, "blue"], breaks = res))
g.cut <- as.factor(cut(dat[, "green"], breaks = res))
cols <- c(blues[b.cut], greens[g.cut])
cols
These colors can be used for plotting. Note that I changed the name of your input data, because data is a function and a good programming practice is to avoid overwriting functions with variables.
barplot(dat, beside = TRUE, col = cols)
You can modify last line by adding a color argument col for including color options in the code. I modified the last line by adding appropriate hexagonal codes of the colors as given below:
barplot(data, beside = TRUE,col=c("#0000FF","#0066FF","#00FFFF","#ADFF2F","#008000","#006400"))
This is the plot I got.
The modified code is posted below. Change the hexagonal color code according to your wish. I referred Color Cycling in R section from datamentor website.
set.seed(12)
data = matrix(sample(1:6, 6) , nrow = 3)
rownames(data) = c("A", "B", "C")
colnames(data) = c("blue", "green")
barplot(data, beside = TRUE,col=c("#0000FF","#0066FF","#00FFFF","#ADFF2F","#008000","#006400"))
Here is a sample script using random numbers instead of real elevation data.
library(gridExtra)
library(spatstat) #im function
elevation <- runif(500, 0, 10)
B <- matrix(elevation, nrow = 20, ncol = 25)
Elevation_Map <- im(B)
custom <- colorRampPalette(c("cyan","green", "yellow", "orange", "red"))
plot(Elevation_Map, col = custom(10), main = NULL)
This is the plot and legend that I get:
This is the legend that I am trying to recreate in R (this one made in Word):
I know this is possible and its probably a simple solution but I've tried using some examples I found online to no avail.
This plot (with real elevation data) is an art piece that will be hung in a gallery, with the elevation plot on 1 board and the legend on a separate board. I tried to get R to plot just the plot without the legend using
plot(Elevation_Map, col = custom(10), main = NULL, legend = NULL)
like I have in the past but for some reason it always plots the legend with the plot. As of right now I'm planning on just cropping the .pdf into 2 separate files to achieve this.
Here are two ways of doing it using other packages:
# example data, set seed to reproduce.
set.seed(1); elevation <- runif(500, 0, 10)
B <- matrix(elevation, nrow = 20, ncol = 25)
#Elevation_Map <- im(B)
custom <- colorRampPalette(c("cyan","green", "yellow", "orange", "red"))
1) Using fields package, image.plot(), it is same "base" graphics::image.default() plot but with more arguments for customisation (but couldn't remove the ticks from legend):
library(fields)
image.plot(B, nlevel = 10, col = custom(10),
breaks = 1:11,
lab.breaks = c("Low Elevation", rep("", 9), "High Elevation"),
legend.mar = 10)
2) Using ggplot package, geom_raster function:
library(ggplot2)
library(reshape) # convert matrix to long dataframe: melt
B_melt <- reshape2::melt(B)
head(B_melt)
ggplot(B_melt, aes(X1, X2, fill = value)) +
geom_raster() +
theme_void() +
scale_fill_gradientn(name = element_blank(),
breaks = c(1, 9),
labels = c("Low Elevation", "High Elevation"),
colours = custom(10))
The code in the original post is using the im class from the spatstat package. The plot command is dispatched to plot.im. Simply look at help(plot.im) to figure out how to control the colour ribbon. The relevant argument is ribargs. Here is a solution:
plot(Elevation_Map, col=custom(10), main="",
ribargs=list(at=Elevation_Map$yrange,
labels=c("Low Elevation", "High Elevation"),
las=1))
I would like to overlay 2 density plots on the same device with R. How can I do that? I searched the web but I didn't find any obvious solution.
My idea would be to read data from a text file (columns) and then use
plot(density(MyData$Column1))
plot(density(MyData$Column2), add=T)
Or something in this spirit.
use lines for the second one:
plot(density(MyData$Column1))
lines(density(MyData$Column2))
make sure the limits of the first plot are suitable, though.
ggplot2 is another graphics package that handles things like the range issue Gavin mentions in a pretty slick way. It also handles auto generating appropriate legends and just generally has a more polished feel in my opinion out of the box with less manual manipulation.
library(ggplot2)
#Sample data
dat <- data.frame(dens = c(rnorm(100), rnorm(100, 10, 5))
, lines = rep(c("a", "b"), each = 100))
#Plot.
ggplot(dat, aes(x = dens, fill = lines)) + geom_density(alpha = 0.5)
Adding base graphics version that takes care of y-axis limits, add colors and works for any number of columns:
If we have a data set:
myData <- data.frame(std.nromal=rnorm(1000, m=0, sd=1),
wide.normal=rnorm(1000, m=0, sd=2),
exponent=rexp(1000, rate=1),
uniform=runif(1000, min=-3, max=3)
)
Then to plot the densities:
dens <- apply(myData, 2, density)
plot(NA, xlim=range(sapply(dens, "[", "x")), ylim=range(sapply(dens, "[", "y")))
mapply(lines, dens, col=1:length(dens))
legend("topright", legend=names(dens), fill=1:length(dens))
Which gives:
Just to provide a complete set, here's a version of Chase's answer using lattice:
dat <- data.frame(dens = c(rnorm(100), rnorm(100, 10, 5))
, lines = rep(c("a", "b"), each = 100))
densityplot(~dens,data=dat,groups = lines,
plot.points = FALSE, ref = TRUE,
auto.key = list(space = "right"))
which produces a plot like this:
That's how I do it in base (it's actually mentionned in the first answer comments but I'll show the full code here, including legend as I can not comment yet...)
First you need to get the info on the max values for the y axis from the density plots. So you need to actually compute the densities separately first
dta_A <- density(VarA, na.rm = TRUE)
dta_B <- density(VarB, na.rm = TRUE)
Then plot them according to the first answer and define min and max values for the y axis that you just got. (I set the min value to 0)
plot(dta_A, col = "blue", main = "2 densities on one plot"),
ylim = c(0, max(dta_A$y,dta_B$y)))
lines(dta_B, col = "red")
Then add a legend to the top right corner
legend("topright", c("VarA","VarB"), lty = c(1,1), col = c("blue","red"))
I took the above lattice example and made a nifty function. There is probably a better way to do this with reshape via melt/cast. (Comment or edit if you see an improvement.)
multi.density.plot=function(data,main=paste(names(data),collapse = ' vs '),...){
##combines multiple density plots together when given a list
df=data.frame();
for(n in names(data)){
idf=data.frame(x=data[[n]],label=rep(n,length(data[[n]])))
df=rbind(df,idf)
}
densityplot(~x,data=df,groups = label,plot.points = F, ref = T, auto.key = list(space = "right"),main=main,...)
}
Example usage:
multi.density.plot(list(BN1=bn1$V1,BN2=bn2$V1),main='BN1 vs BN2')
multi.density.plot(list(BN1=bn1$V1,BN2=bn2$V1))
You can use the ggjoy package. Let's say that we have three different beta distributions such as:
set.seed(5)
b1<-data.frame(Variant= "Variant 1", Values = rbeta(1000, 101, 1001))
b2<-data.frame(Variant= "Variant 2", Values = rbeta(1000, 111, 1011))
b3<-data.frame(Variant= "Variant 3", Values = rbeta(1000, 11, 101))
df<-rbind(b1,b2,b3)
You can get the three different distributions as follows:
library(tidyverse)
library(ggjoy)
ggplot(df, aes(x=Values, y=Variant))+
geom_joy(scale = 2, alpha=0.5) +
scale_y_discrete(expand=c(0.01, 0)) +
scale_x_continuous(expand=c(0.01, 0)) +
theme_joy()
Whenever there are issues of mismatched axis limits, the right tool in base graphics is to use matplot. The key is to leverage the from and to arguments to density.default. It's a bit hackish, but fairly straightforward to roll yourself:
set.seed(102349)
x1 = rnorm(1000, mean = 5, sd = 3)
x2 = rnorm(5000, mean = 2, sd = 8)
xrng = range(x1, x2)
#force the x values at which density is
# evaluated to be the same between 'density'
# calls by specifying 'from' and 'to'
# (and possibly 'n', if you'd like)
kde1 = density(x1, from = xrng[1L], to = xrng[2L])
kde2 = density(x2, from = xrng[1L], to = xrng[2L])
matplot(kde1$x, cbind(kde1$y, kde2$y))
Add bells and whistles as desired (matplot accepts all the standard plot/par arguments, e.g. lty, type, col, lwd, ...).