I have a bivariate plot of axis2 against axis1 (data below). I would like
to use different size, type and color for points in the plot for the point
coming from different region. For some reasons, I cannot get it done. Below
is my code.
col <- rep(c("blue", "red", "darkgreen"), c(16,
16, 16))
## Choose different size of points
cex <- rep(c(1, 1.2, 1), c(16, 16, 16))
## Choose the form of the points (square, circle, triangle and
diamond-shaped
pch <- rep(c(15, 16, 17), c(16, 16, 16))
plot(axis1, axis2, main="My plot", xlab="Axis 1",
ylab="Axis 2",
col=c(Category, col), pch=pch, cex=cex)
legend(4, 12.5, c("NorthAmerica", "SouthAmerica",
"Asia"), col = col,
pch = pch, pt.cex = cex, title = "Region")
I also prefer a control on what kind of point I want to use for different
levels of Region. Something like this:
legend(4,12.5, col(levels(Category), Asia="red",
NorthAmerica="blue",
SouthAmerica="green"))
Thanks,
Kumar
Region axis1 axis2 NorthAmerica 5 14 NorthAmerica 8 13 NorthAmerica 8
11 NorthAmerica 6 11 NorthAmerica 5 13 SouthAmerica 8 17 SouthAmerica 7
16 SouthAmerica 7 13 SouthAmerica 8 14 SouthAmerica 6 17 Asia 7 13 Asia
6 15 Asia 7 14 Asia 5 13 Asia 4 16
[[alternative HTML version deleted]]
I think there may 3 legends should be added in your plot
the argument col, pch and pt.cex should be in the same length with legend,
but the objects col, pch
and cex you defined former have 16*3 length. I guess the follow codes may
work
col <- rep(c("blue", "red", "darkgreen"), c(16,
16, 16))
## Choose different size of points
cex <- rep(c(1, 1.2, 1), c(16, 16, 16))
## Choose the form of the points (square, circle, triangle and
diamond-shaped
pch <- rep(c(15, 16, 17), c(16, 16, 16))
plot(axis1, axis2, main="My plot", xlab="Axis 1",
ylab="Axis 2",
col=c(Category, col), pch=pch, cex=cex)
legend(4, 12.5, c("NorthAmerica", "SouthAmerica",
"Asia"), col unique(col),
pch = unique(pch), pt.cex = unique(cex), title = "Region")
--
View this message in context:
http://r.789695.n4.nabble.com/Legend-based-on-levels-of-a-variable-tp4536796p4536868.html
Sent from the R help mailing list archive at Nabble.com.
Hi> > I have a bivariate plot of axis2 against axis1 (data below). I wouldlike> to use different size, type and color for points in the plot for thepoint> coming from different region. For some reasons, I cannot get it done.Below> is my code. > > col <- rep(c("blue", "red", "darkgreen"), c(16, 16, 16)) > ## Choose different size of points > cex <- rep(c(1, 1.2, 1), c(16, 16, 16)) > ## Choose the form of the points (square, circle, triangle and > diamond-shaped > pch <- rep(c(15, 16, 17), c(16, 16, 16)) > > plot(axis1, axis2, main="My plot", xlab="Axis 1", ylab="Axis 2", > col=c(Category, col), pch=pch, cex=cex) > legend(4, 12.5, c("NorthAmerica", "SouthAmerica", "Asia"), col = col, > pch = pch, pt.cex = cex, title = "Region") > > I also prefer a control on what kind of point I want to use fordifferent> levels of Region. Something like this: > legend(4,12.5, col(levels(Category), Asia="red", NorthAmerica="blue", > SouthAmerica="green"))So why you do not use Region and/or Category for automatic point colouring/size/type. Without data I can only use built in one. with(iris, plot(Sepal.Length, Sepal.Width, col= as.numeric(Species))) legend("topright", legend=levels(iris$Species), pch=19, col=1:3) Regards Petr> > Thanks, > Kumar > > Region axis1 axis2 NorthAmerica 5 14 NorthAmerica 8 13 NorthAmerica8> 11 NorthAmerica 6 11 NorthAmerica 5 13 SouthAmerica 8 17 SouthAmerica7> 16 SouthAmerica 7 13 SouthAmerica 8 14 SouthAmerica 6 17 Asia 7 13Asia> 6 15 Asia 7 14 Asia 5 13 Asia 4 16 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Ein eingebundener Text mit undefiniertem Zeichensatz wurde abgetrennt. Name: nicht verf?gbar URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120406/c6840426/attachment.pl>
Thanks,
anyway, using build-in R features is preferable for colours
with(data, plot(axis1, axis2, col= c("red", "blue",
"green")[as.numeric(data$Region)]))
legend("topright", legend=levels(data$Region), fill=
c("red", "blue",
"green"))
although sometimes can be preferable to get advantage of grid graphic
library(ggplot2)
p<-ggplot(data, aes(x=axis1, y=axis2, colour=Region))
p+geom_point()
Regards
Petr
>
> He provided data, yet in an inconvenient way at the bottom of his post.
>
> Kumar, please use dput() to provide data to the list, because its much
> easier to import:
> dput(data) ## name data is made up by me
>
> structure(list(Region = structure(c(2L, 2L, 2L, 2L, 2L, 3L, 3L,
> 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L), .Label = c("Asia",
"NorthAmerica",
> "SouthAmerica"), class = "factor"), axis1 = c(5L, 8L,
8L, 6L,
> 5L, 8L, 7L, 7L, 8L, 6L, 7L, 6L, 7L, 5L, 4L), axis2 = c(14L, 13L,
> 11L, 11L, 13L, 17L, 16L, 13L, 14L, 17L, 13L, 15L, 14L, 13L, 16L
> )), .Names = c("Region", "axis1", "axis2"),
class = "data.frame",
row.names = c(NA, > -15L))
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.