[R] Different symbols for select points
Sarah Goslee
sarah.goslee at gmail.com
Mon Oct 24 23:10:17 CEST 2011
On Mon, Oct 24, 2011 at 4:52 PM, lauren mcdonagh
<cleverlollipop at yahoo.ie> wrote:
> Dear R users,
>
> I hope you can help me as you have done before.
>
> I was wondering if I can make some of the points on my graph a different colour and symbol? Some of my soil samples are enriched and I wanted them to be shown with a red triangle and the samples which are not enriched I wanted to show with a blue square. I can't seem to find a way of doing this if there is a way?
You can use a vector for par and col, instead of a single value:
plot (y~x,
xaxt = "n", type = "p", xlab = "Distance (m)",
ylab = "Concentrations (mg/kg)", cex.axis = .8, xlim = c(0,3000),
ylim = c(0,800), col=c(rep(2, 2), rep(4, 2), rep(2, 7)), pch =
c(rep(17, 2), rep(15, 2), rep(17, 7)), bg = 2, sub =
"Zinc",
font.sub = 2)
If there's some criterion within your data that describes enriched and
unenriched, you can use that to create the vector, instead of doing it
manually as I did.
isEnriched <- c(1,1,2,2,1,1,1,1,1,1,1)
col=c("red", "blue")[isEnriched]
and
pch=c(2, 4)[isEnriched]
Alternatively, you can subset the data and use points() to add
different sets with different parameters.
Thanks for providing a reproducible example.
Sarah
>
> My script:
>
> x
> <- c(0, 50, 100, 250, 500, 750, 1000, 1500, 2000, 2500, 3000)
> y
> <- c(450, 109, 27,21,206,138,97,185,77,670,80)
> plot (y~x,
> xaxt = "n", type = "p", xlab = "Distance (m)",
> ylab = "Concentrations (mg/kg)", cex.axis = .8, xlim = c(0,3000),
> ylim = c(0,800), col =2, pch = 17, bg = 2, sub = "Zinc",
> font.sub = 2)
> abline(h=c(63),
> lty = 2, col = "green", lwd =1)
> abline(h=c(140),
> lty = 3, col = "blue", lwd = 1)
> abline(h=c(720),
> lty = 1, col ="red", lwd = 2)
> legend(1300,
> 700, c("Control", "Dutch List - Optimum", "Dutch List
> - Action"), col = c(3,4,2), lty = c(2,3,1), cex = 0.8, lwd = c(1,1,2),
> inset = 1)
> axis(1,
> at = x, cex.axis=.7, las=2)
> text(0,450,
> "mine average", pos = 4, col = "black", cex= 0.8)
>
> If possible I would like coordinates (100, 27) and (250, 21) to be shown as blue squares (col = 4, bg = 4, pch = 15).
>
> Hope you can help!
> Lauren
> [[alternative HTML version deleted]]
>
>
--
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-help
mailing list