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)
Related
I'm using par(mfrow) to generate a multi-panel plot of three separate graph output objects. Sample code below represents a very simplified version of the objects I have.
How can I save these plots as a single object with ggsave? I've tried naming the par(mfrow) as an object and plotting it, but that doesn't seem to work.
Any advice on alternative ways of generating/saving a multi-panel plot is also welcome! Please let me know if I can clarify the question or example. Thanks!
par(mfrow = c(1,3), mar = c(10, 5, 5, 3), xpd = TRUE)
hist(x = rnorm(100), col = "skyblue", main = "X")
hist(x = rnorm(50), col = "green", main = "Y")
legend("bottom", c("Blue", "Green", "Purple"),
title = "Sample Data", horiz = TRUE, inset = c(0, -0.4),
col = c("skyblue", "green", "purple"), pch = rep(15,2),
bty = "n", pt.cex = 1.5, text.col = "black")
hist(x = rnorm(75), col = "purple", main = "Z")
I would suggest using svglite over svg if you want to edit the graph using Inkscape or a similar program since you will not be able to edit the text (change the text, font, or size) in a file produced by svg. Here is an example with a few edits to your original code:
library(svglite)
svglite("MyPlots.svg", width=8, height=6)
par(mfrow = c(1,3), mar = c(10, 5, 5, 3), xpd = TRUE, mgp=c(1.75, .75, 0))
hist(x = rnorm(100), col = "skyblue", main = "X")
hist(x = rnorm(50), col = "green", main = "Y")
legend("bottom", c("Blue", "Green", "Purple"),
title = "Sample Data", horiz = TRUE, inset = c(0, -0.2),
col = c("skyblue", "green", "purple"), pch = rep(15,2),
bty = "n", pt.cex = 1.5, text.col = "black")
hist(x = rnorm(75), col = "purple", main = "Z")
dev.off()
I want a dot plot for mydf below
mydf <- data.frame(city=c(rep(c("Rome","NY","LA"),3)),
old=c(11,23,13,24,12,13.5,15,17,22),
new=c(12,22,13.5,25,14,15,12,17,14),
method=c("a","a","a","b","b","b","c","c","c"))
my_cols <- c("red", "blue", " dark green")
grps <- as.factor(mydf$method)
dotchart(mydf$old, labels = mydf$city,
groups = grps, gcolor = my_cols,
color = my_cols[grps],
cex = 0.6, pch = 19, xlab = "value")
meth1<- mydf[mydf$method=="a",]
meth2<- mydf[mydf$method=="b",]
meth3<- mydf[mydf$method=="c",]
points(meth1$new, 1:nrow(meth1), col = "orange", pch = 16, cex = 0.6)
points(meth2$new, 1:nrow(meth2), col = "light blue", pch = 16, cex = 0.6)
points(meth2$new, 1:nrow(meth2), col = "green", pch = 16, cex = 0.6)
before adding points(), I get plot below, which is what I want as a basis.
but when I add points, all of them appear in the bottom part of the plot. I want the "new" values corresponding to each method appear in its own part of the plot and a segment line connects the old and the new values accordingly.
how can I do that in my code? thanks for any help with this.
The y-axis in the dotchart, under the hood, is really just integers. So you have to provide the correct y-axis values to points():
points(meth1$new, 11:13, col = "orange", pch = 16, cex = 0.6)
points(meth2$new, 6:8, col = "light blue", pch = 16, cex = 0.6)
points(meth2$new, 1:3, col = "green", pch = 16, cex = 0.6)
I reached those values by just eyeballing the plot and counting up. Note that not all these points will appear if they are outside the range of the original dotchart call. You can adjust that by setting xlim = c(11,25) or something appropriate in the dotchart call.
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 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).