I have a dataset that looks like that, I want to plot the exact same bar plot in R.
This is what I've been trying:
barplot(as.matrix(table[,2:8]), beside = T, ylim= c(0,1),
col = c("red", "blue", "green", "yellow",
"orange", "purple", "cyan", "grey",
"deeppink", "red4", "black", "brown"))
I get a bar plot, but now when I want to change the labels instead of a,b,c...g to this:
(-0.20,-0.15)
(-0.15,-0.10)
.
.
.
(0.20,0.25)
I get an X character in front of every label I was trying to write:
Thank you.
Related
I am using the beanplot function using the following code:
library(beanplot)
beanplot(Grainyield~Year*Tmnt,las = 2,
data = maize,
xlab = "Factors Year.Water Treatment ",
ylab = "Yield [kg/ha]",
col = list("yellow", "orange", "green", "blue"))
legend("bottomleft",
bty="n",
c("2008","2009","2010","2011"),
fill = c("yellow", "orange", "green", "blue"))
The x-axis labels are incorrect and I have tried to formatting the x-axis labels using the following code, but I keep receiving errors:
axis(1, at=2008.1:2011.6,
labels=c[1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6])
Any suggestions?
I'm fairly new to R.
I'm trying to create a density plot, which wasn't problem thanks to previous questions & answers here.
My current problem is the graphs's legend. I've assigned the wanted colors (col = c("red", "orange", "yellow", "green", "blue", "purple")) but they don't show on the legend itself, instead I get random colors.
I think it's important to mention that there are no errors after running the code & i've checked that all these colors are available here.
This the density plot code, along with the legend info.
plot(density(df$a1), col = "red", xlim = c(0, 1000), ylim = c(0, 0.004))
lines(density(df$a2), col = "orange")
lines(density(df$a3), col = "yellow")
lines(density(df$a4), col = "green")
lines(density(df$a5), col = "blue")
lines(density(df$a6), col = "purple")
legend(x = "topright", legend = names(df), fill = 1:6, col = c("red", "orange", "yellow", "green", "blue", "purple"))
Yet, the result is this:
Thank you!
fill specifies color for boxes while colspecifies color for points and lines.
In your case, the code should be:
legend(x = "topright", legend = names(df), lty=1, col = c("red", "orange", "yellow", "green", "blue", "purple"))
lty=1 indicates that you want solid lines in place of boxes (lty stands for line type).
"red", "red", "red", "green", "green", "blue"
I have tried using rep, etc but I can only get one the colors.
use rep and/or c, twice at most, each with a max of 2 arguments.
rep(c("red", "green", "blue"), c(3, 2, 1))
I would like to replicate the annotation image seen below, by creating a matrix of colors based on annotations I have created:
ann1 <- c("blue", "red", "green", "red")
ann2 <- c("black", "gray", "yellow", "white")
ann3 <- c("orange", "blue", "pink", "green")
object <- cbind(ann1, ann2, ann3)
Which function can I use? (you don't need you to do the work!)
Finished product for future reference:
ann1 <- c("blue", "blue", "red", "red")
ann2 <- c("black", "gray", "black", "black")
ann3 <- c("red", "red", "red", "green")
object <- cbind(ann1, ann2, ann3)
colnames(object) <- c("group1", "group2", "group3")
image(matrix(1:length(object), nrow(object), ncol(object)), col=object, yaxt='n', xaxt='n', yaxs="i")
par(las=1)
increment <- seq(0,1, by=(1/(ncol(object)-1)))
increment <- increment[1:ncol(object)]
axis(side=2, at=increment, labels=colnames(object))
One way to get you started...
ann1 <- c("blue", "red", "green", "red")
ann2 <- c("black", "gray", "yellow", "white")
ann3 <- c("orange", "blue", "pink", "green")
object <- cbind(ann1, ann2, ann3)
image(t(matrix(1:12, 4, 3)), col=object)
I am using heatmaps.plus to create heatmaps with RowSideColors. The only thing I can't seem to figure out is how to make a legend for the RowSideColors (for example, green is Group1 and black is Group2). Any help would be greatly appreciated, thank you!
You should use legend for this.
library("heatmap.plus")
#Create dummy data
data <- replicate(10, rnorm(10))
rsc <- c("green", "green", "black", "green", "green", "black", "black", "green", "green", "black")
rsc <- cbind(rsc, rsc)
colnames(rsc) <- c("Groups", "")
#Plot
heatmap.plus(data, RowSideCol = rsc)
#Legend on position (40, 2)
legend(40, 2,legend=c("Title","","Group1","Group2"), fill=c("white", "white", "green","black"), border=FALSE, bty="n", y.intersp = 0.7, cex=0.7)