I have a data frame with 3 columns and I want to order the entire list by one column and then plot. I used order() and it does order the data set but when I plot it is as if the set is as it was originally. I also can't figure out how to plot two sets of data on the same graph. I have, Occupation American.Workers Foreign.Workers Accountant 12 2 Engineer 45 54 Doctor 50 37 I want to be able to order American.Workers and then plot(Occupation,American.Workers) and plot(Occupation,Foreign.Workers) on the same graph. -- View this message in context: http://n4.nabble.com/Keeping-the-order-of-data-set-when-plotting-tp1574535p1574535.html Sent from the R help mailing list archive at Nabble.com.
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Can you provide the data and the commands that you were using. When providing the data, please use 'dput'. Most likely it might be because one of the things you are trying to plot is a factor, but unless we can see the data and commands, it is hard to tell. To add a line to a plot, you would do the following by making sure you have the correct limits for y: plot(Occupation,American.Workers, ylim=range(c(American.Workers, Foreign.Workers))) lines(Occupation,Foreign.Workers) On Mon, Mar 1, 2010 at 9:53 PM, cosinenonqua <sergeygoder at gmail.com> wrote:> > I have a data frame with 3 columns and I want to order the entire list by one > column and then plot. I used order() and it does order the data set but when > I plot it is as if the set is as it was originally. I also can't figure out > how to plot two sets of data on the same graph. I have, > > Occupation ? ? ? ? ? ? ?American.Workers ? ? ? ?Foreign.Workers > Accountant ? ? ? ? ? ? ?12 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2 > Engineer ? ? ? ? ? ? ? ?45 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?54 > Doctor ? ? ? ? ?50 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?37 > > I want to be able to order American.Workers and then > plot(Occupation,American.Workers) and plot(Occupation,Foreign.Workers) on > the same graph. > -- > View this message in context: http://n4.nabble.com/Keeping-the-order-of-data-set-when-plotting-tp1574535p1574535.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Hi: order() simply returns the index vector that corresponds to the ordering, not the ordering per se. Besides, when you read in the data, Occupation is read in as a factor that will by default order the levels alphabetically. If you want a different ordering, you can redefine the factor; e.g., df$Occupation <- factor(df$Occupations, levels = c('Doctor', 'Engineer', 'Accountant')) It appears that you want to do something like a bar chart. The code below is one way to do this with base graphics, but this is readily done in lattice and ggplot2 as well. I refer to your toy data below as df and use the given ordering for occupation.> str(df)'data.frame': 3 obs. of 3 variables: $ Occupation : Factor w/ 3 levels "Accountant","Doctor",..: 1 3 2 $ American.Workers: int 12 45 50 $ Foreign.Workers : int 2 54 37 # Create a matrix of frequencies, using occupation as the row names:> m <- as.matrix(df[, -1]) > rownames(m) <- df[, 1] > mAmerican.Workers Foreign.Workers Accountant 12 2 Engineer 45 54 Doctor 50 37 # Create a bar chart (this one is side-by-side, the default (beside = FALSE) # will stack instead)> barplot(t(m), beside = TRUE, col = c('blue', 'red')) > box()# Add a legend> legnames <- c('American', 'Foreign')# Use the mouse to locate the legend on the graphics surface> legend(locator(1), legend = legnames, fill = c('blue', 'red'))HTH, Dennis On Mon, Mar 1, 2010 at 6:53 PM, cosinenonqua <sergeygoder@gmail.com> wrote:> > I have a data frame with 3 columns and I want to order the entire list by > one > column and then plot. I used order() and it does order the data set but > when > I plot it is as if the set is as it was originally. I also can't figure out > how to plot two sets of data on the same graph. I have, > > Occupation American.Workers Foreign.Workers > Accountant 12 2 > Engineer 45 54 > Doctor 50 37 > > I want to be able to order American.Workers and then > plot(Occupation,American.Workers) and plot(Occupation,Foreign.Workers) on > the same graph. > -- > View this message in context: > http://n4.nabble.com/Keeping-the-order-of-data-set-when-plotting-tp1574535p1574535.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
On 03/02/2010 01:53 PM, cosinenonqua wrote:> > I have a data frame with 3 columns and I want to order the entire list by one > column and then plot. I used order() and it does order the data set but when > I plot it is as if the set is as it was originally. I also can't figure out > how to plot two sets of data on the same graph. I have, > > Occupation American.Workers Foreign.Workers > Accountant 12 2 > Engineer 45 54 > Doctor 50 37 > > I want to be able to order American.Workers and then > plot(Occupation,American.Workers) and plot(Occupation,Foreign.Workers) on > the same graph.Hi cosinenonqua, This seems to do what you are asking, but you might not want this sort of plot: plot(cosinenonqua$American.Workers,col=2,xaxt="n",ylim=c(0,55)) points(cosinenonqua$Foreign.Workers,col=4) axis(1,at=1:3,labels=cosinenonqua$Occupation) legend(1.7,30,c("American","Foreign"),col=c(2,4),pch=1) Jim