Quick
Quick

Reputation: 21

Changing scatterplot symbols according to a text string in R

I'm new to R (and coding in general) and am having trouble with a scatterplot. I have four groups in my data (VH, H, M, L) that is in a column called hdi. I want to have the symbols in my scatterplot change according to what string is in the hdi column (i.e. Star if VH, circle if M, etc.). Something like the "group by" in minitab.

I've found the pch command, but can't figure out how to split it up.

Thanks

Edit> sorry, this is the first question I've asked here. Here's an edited version of what I have so far, and I want to have the shapes determined by which of the 4 text strings it has in group1csv$hdi

producedpercent <- group1csv$Percentage.of.requests.where.some.data.produced * 100 hdinumber <- group1csv$UN.HDI.number

windows() plot(hdinumber, producedpercent, xlab="", ylab="", las=1 )

Upvotes: 0

Views: 1916

Answers (3)

Wave
Wave

Reputation: 1266

I first made you up some fake data to work with (please provide some next time)

d=data.frame(x=1:12,y=rnorm(12,10,2),hdi=rep(c("VH","H","M","L")))

For the plot it is easy to work with the ggplot package.

library(ggplot2)
ggplot(d,aes(x=x,y=y))+
     geom_point(aes(shape=hdi))+
     scale_shape_manual(values=c(2,3,4,5))

First you define the data and the x and y aestethics (coordinates), than the geom_point() comment adds the points and with scale_shape_manual you can change the shapes (I took random values). Similarly, you can also use color=, size=,... to make the difference between the groups.

Upvotes: 2

Ujjwal
Ujjwal

Reputation: 3168

Try:

plot(data$x, data$y, pch=data$hdi)

where

  • "data" is the name of your data frame
  • "x" is the name of column you want to plot on x axis
  • "y" is the name of column you want to plot on y axis

Upvotes: 2

rnso
rnso

Reputation: 24593

Try with ggplot:

library(ggplot2)
ggplot(ddf)+geom_point(aes(x=hdi, y=values, shape=hdi), size=5)

enter image description here

Upvotes: 1

Related Questions