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 *********************************************************************** This message may contain confidential and privileged information. If you are not the intended recipient you should not disclose, copy or distribute information in this e-mail or take any action in reliance on its contents. To do so is strictly prohibited and may be unlawful. Please inform the sender that this message has gone astray before deleting it. Thank you. 2008 marks the 60th anniversary of the NHS. It's an opportunity to pay tribute to the NHS staff and volunteers who help shape the service, and celebrate their achievements. If you work for the NHS and would like an NHSmail email account, go to: www.connectingforhealth.nhs.uk/nhsmail
Sandy, You can re-order a factor with df$Eyeball<-factor(df$Eyeball, levels=c("Normal", "Mild", "Moderate", "Severe"), ordered=T) (assuming df is your data frame and that you want an _ordered_ factor; the latter is not essential to your plots) Incidentally, "NULL" isn't a particularly friendly item to find in a data frame. NULL often implies "I'm not here at all" while NA says "I exist but I'm a missing value". For an example of when it might matter, try length(c(1,2,NULL,3)) #versus length(c(1,2,NA,3)) Steve E>>> Sandy Small <sandy.small at nhs.net> 01/08/2008 16:21:29 >>>Hi I'm sure this question has been asked before but I can't find it in the archives.> I want to plot them in the order "Normal", "Mild","Moderate", "Severe" so that the trend (or not) is obvious. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
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 > SandyWe 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
I'm not really clear on what you want here. Are you talking about plotting multiple data points for each value ? In that case something like boxplot might be what you want. Otherwise if you just wish to plot a data point for each occurance of Normal etc then this will work but I'm not sure how appropriate it is as a way to look at the data. If the graph looks a bit squashed, just stretch it. x <- '"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 NA Normal 27.9 NA 68.1 Normal' xx <- read.table(textConnection(x), header=TRUE, as.is=TRUE); xx df1 <- data.frame(Eyeball= c("Normal", "Mild", "Moderate", "Severe"), nums= c(1:4)) df2 <- merge(xx, df1, by="Eyeball", all=TRUE) plot(df2[,2], xaxt="n", xlab="Eyeball", ylab="Measured") mtext(1, text=df2[,1], at=1:length(df2[,1]), line = 1 , cex=.7 ) __________________________________________________________________ [[elided Yahoo spam]]
Many thanks I was sure it was simple. That was exactly what I wanted. I should have clarified that I was looking for a box-plot. Thanks to all who responed. Sandy S Ellison wrote: Sandy, You can re-order a factor with df$Eyeball<-factor(df$Eyeball, levels=c("Normal", "Mild", "Moderate", "Severe"), ordered=T) (assuming df is your data frame and that you want an _ordered_ factor; the latter is not essential to your plots) Incidentally, "NULL" isn't a particularly friendly item to find in a data frame. NULL often implies "I'm not here at all" while NA says "I exist but I'm a missing value". For an example of when it might matter, try length(c(1,2,NULL,3)) #versus length(c(1,2,NA,3)) Steve E Sandy Small [1]<sandy.small at nhs.net> 01/08/2008 16:21:29 >>> Hi I'm sure this question has been asked before but I can't find it in the archives. I want to plot them in the order "Normal", "Mild", "Moderate", "Severe" so that the trend (or not) is obvious. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:29}} References 1. mailto:sandy.small at nhs.net