[R] Plotting ordered nominal data
Marc Schwartz
marc_schwartz at comcast.net
Fri Aug 1 18:55:03 CEST 2008
on 08/01/2008 10:21 AM Sandy Small wrote:
> Hi
> I'm sure this question has been asked before but I can't find it in the
> archives.
> I have a data frame which includes interval and ordered nominal results.
> It looks something like
>
> "Measured" "Eyeball"
> 46.5 Normal
> 43.5 Mild
> 56.2 Normal
> 41.1 Mild
> 37.8 Moderate
> 12.6 Severe
> 17.3 Moderate
> 39.1 Normal
> 26.7 Mild
> NULL Normal
> 27.9 NULL
> 68.1 Normal
>
> I want to plot the Measured value against the "Eyeball" value but if I
> simply plot it the "Eyeball" values are plotted in alphabetical order. I
> do not want to change the "names" as "Normal, Mild, Moderate, Severe"
> are standard but I want to plot them in the order "Normal", "Mild",
> "Moderate", "Severe" so that the trend (or not) is obvious.
>
> Any help would be much appreciated.
> Many thanks
> Sandy
We are going to need a bit more info.
What type of plot?
Point estimates of the means per level of severity?
Boxplot?
Some possibilities:
# Read in the data from the clipboard, converting the "NULL"s to NAs
DF <- read.table("clipboard", header = TRUE, na.strings = "NULL")
> DF
Measured Eyeball
1 46.5 Normal
2 43.5 Mild
3 56.2 Normal
4 41.1 Mild
5 37.8 Moderate
6 12.6 Severe
7 17.3 Moderate
8 39.1 Normal
9 26.7 Mild
10 NA Normal
11 27.9 <NA>
12 68.1 Normal
# Change the factor ordering and include NA as a level
DF$Eyeball <- factor(DF$Eyeball,
levels = c("Normal", "Mild", "Moderate", "Severe",
NA),
exclude = NULL)
# Do a boxplot
boxplot(Measured ~ Eyeball, data = DF)
# Plot means by severity
Res <- tapply(DF$Measured, list(DF$Eyeball), mean, na.rm = TRUE)
plot(Res, pch = 19, ylab = "Mean", xlab = "Severity", xaxt = "n")
axis(1, at = 1:5, paste(names(Res)))
See ?factor, ?plot.default, ?boxplot and ?axis
HTH,
Marc Schwartz
More information about the R-help
mailing list