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?
Related
I am trying to put grids in my barplots, but they appear in front of the data and not in the background. I tried to fix this using
panel.first = grid()
As for the data I am trying to plot, the first column consists of the year numbers (2014-2021) and the second columns are the corresponding values (all vector classes are numeric). When trying to plot using the following code:
par(mfrow=c(1,2))
barplot(mean_trend[,2],names.arg = mean_trend[,1],col="skyblue",ylim = c(0.1,95),cex=0.8,cex.names=0.85,las=2,cex.lab=0.85,lwd=1.5,panel.first = grid())
mtext(side=2,line=2.3, "Average amount in mm", font=2, cex=0.8)
box(lwd=1.5)
barplot(freq_trend[,2],names.arg = freq_trend[,1],col="skyblue",ylim = c(2,4500),cex=0.85,cex.names=0.85,las=2,cex.lab=0.85,lwd=1.5,panel.first = grid())
box(lwd=1.5)
mtext(side=2,line=3.3, "Average flood frequency", font=2, cex=0.8)
I obtain the following result
As you can see, the grid is now behind the plotted data, but exceeds the box/plot limits. How can I fix this?
Kind regards
As you didn't add a data mean_trend - I give an example with other data.
About add and others arguments - you can read ?barplot
# One row, two columns
par(mfrow = c(1, 2))
#PLOT1
barplot(table(mtcars$cyl), main = "PLOT 1", col = c("yellow", "green", "red"), ylim = c(0, 15))
grid(nx = NULL, ny = NULL, lwd = 1, lty = 1, col = "gray")
barplot(table(mtcars$cyl), col = c("yellow", "green", "red"), ylim = c(0, 15), add = TRUE)
#PLOT2
barplot(table(mtcars$cyl), main = "PLOT 2", col = c("yellow", "green", "red"), ylim = c(0, 15))
grid(nx = NULL, ny = NULL, lwd = 1, lty = 1, col = "gray")
barplot(table(mtcars$cyl), col = c("yellow", "green", "red"), ylim = c(0, 15), add = TRUE)
I created a plot with two lines without any problem. The creation of the legend also worked without any problems. I just need to modify one line of the legend as it is dotted in the plot:
legend("bottom", legend = c("y", "y2", "A"), col = c("red", "orange", "blue"),
lwd=1, cex = 0.3)
so the line for A is dotted, how can I code this into R?
You can specify lty= , in the legend function, just that it's better to specify it before plotting, for example:
set.seed(111)
df = data.frame(x = 1:10, y =runif(10),y2=runif(10)+1,A=runif(10)-1)
col = c("red", "orange", "blue")
names(col) = c("y","y2","A")
linetypes = c(1,4,8)
names(linetypes) = c("y","y2","A")
plot(NULL,xlim=c(1,10),ylim=c(-2,2))
for(i in c("y","y2","A")){
lines(df$x,df[,i],col=col[i],lty=linetypes[i])
}
legend("bottom", legend = c("y", "y2", "A"),
col = c("red", "orange", "blue"), lty = linetypes,
lwd=1)
I have the following code:
require(lattice)
data <- data.frame(x = c(1,1,2,3,4,5), y = c(5,1,4,6,1,7),
color = c("red", "red", "black", "blue", "yellow", "red"))
data$size <- data$x*data$y*0.2
xyplot(data$y ~ data$x, xlab = "x", ylab = "y", cex = data$size, ylim = c(0,11), xlim = c(0,6))
As you can see, this gives scatter plot with conditional sizing of circles. What I want to do is to fill the circles by condition as well (coloring provided by data$color). I tried to use fill option, but it fails to give me what I want. I will appreciate any suggestions on how to accomplish that.
We can set the pch argument to be solid circles.
library(lattice)
xyplot(data$y ~ data$x, xlab = "x", ylab = "y", cex = data$size, ylim = c(0,11), xlim = c(0,6),
col = data$color, pch = 19)
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).
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.