How to name the correct x-axis when plotting scatterplot in R

时间:2015-12-10 01:14:49

标签: r plot dataframe scatter-plot

I have the following passing yard corresponding to each team data. At first I import the data and modify the data frame.

> data = read.table("passing.txt",header = TRUE)
> data$base = data[,1]
> data = data[2:12]
> data
   X2005Season X2006Season X2007Season X2008Season X2009Season X2010Season
1         4437        3662        4065        4674        4016        2921
2         2679        2371        3296        3336        3571        3567
3         3088        3435        3035        2808        3419        3335
4         2002        3281        3362        3061        3473        3015
5         3766        3795        4334        3813        4180        4124
6         3578        3058        3154        3177        4019        3885
7         2848        3820        3878        2960        3168        3810
8         3106        3027        3463        3025        3490        3913
9         3677        4119        3755        3911        4089        3906
10        2926        3733        3071        3301        4148        3601
11        4036        3962        3233        2947        2686        3268
12        1898        2688        2320        3379        3052        3356
13        3047        2898        3726        2380        2076        2989
14        4096        4308        4033        4094        4515        4609
15        3341        3836        4105        3789        4287        4042
16        3810        3000        3181        3129        2922        2968
17        3495        3262        3005        3858        4338        4519
18        3227        2799        3584        4471        3627        4038
19        2642        3153        3014        3303        2380        3242
20        4120        3400        4731        3569        4436        3847
21        3582        2420        2631        2369        2557        3180
22        3597        2596        2878        2819        3031        3107
23        2515        2719        2634        3040        2515        3158
24        3146        3123        2745        2956        4156        3097
25        3300        3287        3031        3632        3170        3527
26        3343        4503        4314        4977        4355        4441
27        3820        3833        4012        2406        2890        3767
28        3458        3054        3964        2617        3503        3341
29        2890        2798        3357        3619        2975        3361
30        3271        3264        2735        3158        2799        2289
31        3190        2882        3328        3332        3356        3065
32        2237        2778        3751        4267        4654        4144
   X2011Season X2012Season X2013Season X2014Season                base
1         3567        3005        4002        3808    ArizonaCardinals
2         4192        4509        4243        4553      AtlantaFalcons
3         3423        3739        3590        3819     BaltimoreRavens
4         3011        2999        4281        3792        ChicagoBears
5         4924        4049        4268        4261     GreenBayPackers
6         4734        3825        3588        4272       NewYorkGiants
7         4814        4927        4482        4030        DetroitLions
8         3773        3422        3751        4047  WashingtonRedskins
9         4110        3791        4110        4356  PhiladelphiaEagles
10        4054        3787        4017        4825  PittsburghSteelers
11        2870        3550        3125        3400         StLouisRams
12        2930        3298        2979        3063   SanFrancisco49ers
13        3090        3435        4047        3465     ClevelandBrowns
14        2995        4128        3725        4894   IndianapolisColts
15        4201        4729        3954        3784       DallasCowboys
16        3080        2713        3340        3182    KansasCityChiefs
17        4426        3295        4328        4098    SanDiegoChargers
18        2434        4534        5444        4661       DenverBroncos
19        3297        2891        2932        2946         NewYorkJets
20        5084        4662        4087        4121  NewEnglandPatriots
21        3962        4084        3340        3275      OaklandRaiders
22        3923        3323        3496        3412     TennesseeTitans
23        3703        3269        3103        3614        BuffaloBills
24        2957        2751        3427        3244    MinnesotaVikings
25        3091        3182        3567        3729       Miamidolphins
26        5347        4997        4918        4764    NewOrleansSaints
27        3340        3578        4136        3421   CincinnatiBengals
28        3105        3031        3236        3250     SeattleSeahawks
29        3650        3983        2820        3297  TampaBayBuccaneers
30        3829        3683        3043        3511    CarolinaPanthers
31        2179        3419        3441        3001 JacksonvilleJaguars
32        3506        3830        3813        3352       HoustonTexans

Now I get the column means for each column with:

> x = colMeans(data[1:10])
> x
X2005Season X2006Season X2007Season X2008Season X2009Season X2010Season 
   3255.250    3277.000    3428.906    3380.531    3495.406    3544.750 
X2011Season X2012Season X2013Season X2014Season 
   3675.031    3700.562    3769.781    3788.969 

However, when I plot the x with

> plot(x)

It gives out the plot below: enter image description here

The y-axis are with correct labels but I do want the x - axis to labels to be 2005, 2006, 2007, 2008, ... , 2014 respectively. I tried to use axis() to modify labels but that did not work. Can anyone tell me what command should I look at or what should I do? Sorry but I am new to R.

2 个答案:

答案 0 :(得分:3)

An alternative ggplot approach would be

require(ggplot2)
# Test data frame
data <- data.frame(matrix(runif(1000, 1000, 3000), ncol=10))
colnames(data) <- paste0("x", 2005:2014, "Season")

ggplot(data.frame(season=colnames(data), mean=colMeans(data)))+ # New DF with the means
  geom_point(aes(x=season, y=mean))+
  theme(axis.text.x=element_text(angle=25, hjust=1)) # Optional

<code>ggplot</code> plot

答案 1 :(得分:2)

first you need to do this

   plot(x,axes=FALSE)

then draw x axis and y axis separately

axis(1,at=seq(2005,2014,1)) #to draw x axis 
axis(2)  #to draw y axis .since your y axis is well computed dont need to use 'at'