R: Plot works, but ggplot doesn't

I have some data from the 2008 election, which I've aggregated in the hopes of making a couple graphics about out-party voting. The data looks like this:

> voting
  voteObama voteMcCain      partyID totalvote outvotenumber outVoteProportion
1  325.5233    16.3212 1-Strong Dem  341.8445       16.3212        0.04774452
2  202.7162    32.6342   2-Weak Dem  235.3504       32.6342        0.13866218
3  201.8810    20.7225   3-Lean Dem  222.6035       20.7225        0.09309153
5   32.2052   143.3616   5-Lean Rep  175.5668       32.2052        0.18343559
6   26.6978   204.2876   6-Weak Rep  230.9854       26.6978        0.11558220
7    9.3775   245.7965 7-Strong Rep  255.1740        9.3775        0.03674943

I'd like to use the ggplot2 library, and have had success with it before. I would expect the following command to give me the graph that I want:

> g <- ggplot(data=voting, aes(x=partyID, y=outVoteProportion))
> g + geom_line()

The graphics window shows up, with all the correct labels and scales, but none of the data shows up. If I plot some random data with ggplot, it works fine, also if I plot this data with plot:

> plot(as.factor(voting$partyID), voting$outVoteProportion)

it also works fine. I'm not sure where the problem lies. Is it in the structure of the voting data frame? Or with the way I'm constructing my ggplot object? I expected this to just work, but apparently I'm missing something.

Edit As requested, the output of dput(voting)

structure(list(voteObama = c(325.523299649358, 202.716199040413, 
201.880999490619, 32.2051995545626, 26.6977999508381, 9.37749981880189
), voteMcCain = c(16.3212000578642, 32.634199783206, 20.7224997878075, 
143.361599132419, 204.287599071860, 245.796498090029), partyID = c("1-Strong Dem", 
"2-Weak Dem", "3-Lean Dem", "5-Lean Rep", "6-Weak Rep", "7-Strong Rep"
), totalvote = c(341.844499707222, 235.350398823619, 222.603499278426, 
175.566798686981, 230.985399022698, 255.173997908831), outvotenumber = c(16.3212000578642, 
32.634199783206, 20.7224997878075, 32.2051995545626, 26.6977999508381, 
9.37749981880189), outVoteProportion = c(0.0477445156257969, 
0.138662181778002, 0.0930915275590001, 0.183435591441075, 0.115582197246219, 
0.0367494333108043)), .Names = c("voteObama", "voteMcCain", "partyID", 
"totalvote", "outvotenumber", "outVoteProportion"), row.names = c("1", 
"2", "3", "5", "6", "7"), class = "data.frame")

Posting comment as answer. Read this FAQ to understand why 'geom_line()` does not produce lines


I can reproduce your problem, and did so with other attempts with ggplot2 and geom_line() for discrete factors. You will get an output which may meet your needs if you do

g + geom_bar()

to get something like

链接地址: http://www.djcxy.com/p/30888.html

上一篇: 将个别点改为组合手段/ SE

下一篇: R:绘图工程,但ggplot没有