0

I'm very new to R and I'm having issues with using ggplot2.

When I first tried to plot some points in my dataset I came out with an x and y axis that had way too many labels, as shown below:

too many labels

But when I tried using scale_x_discrete, it wouldn't show all of the labels I specified.

Here is the code I used:

ggplot(schooldata, aes(Economic.Need.Index, Average.Math.Proficiency)) 
+ geom_point() 
+ scale_x_discrete(name = "Economic Need Index", breaks = c("0", "0.2", "0.4", "0.6", "0.8", "1"), labels = c("0", "0.2", "0.4", "0.6", "0.8", "1")) 
+ scale_y_discrete(name = "Average Math Proficiency", breaks = c("1", "2", "3", "4"), labels = c("1", "2", "3", "4"))

and here is my plot:

It only plotted a couple of the breaks/labels

I know there's probably a very easy fix to this, I'm just pretty inexperienced and I don't know what else to try. Any help would be appreciated, thanks!

J. Oh
  • 617
  • 1
  • 8
  • 14
  • 4
    I would convert all those `coords` to numeric and plot with a continuous scale. – missuse Jun 27 '18 at 18:11
  • 5
    Seems like your `Economic.Need.Index` and `Average.Math.Proficiency` columns in the `schooldata` data.frame were read in as factors rather than numeric values. Did you expect them to be numeric? How did you read in the data? Are there any non-numeric values in there? When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 27 '18 at 18:19
  • 2
    To add to @missuse comment, you're looking for `scale_x_continuous()` (once you've converted the variables to numeric classes). – jordan Jun 27 '18 at 18:33
  • Just checked and they were read in as factors - I used as.numeric() and it seemed to do the trick. I'm sorry my question didn't include a reproducible example, I'll try to add that in the future. Thank you for your help! – J. Oh Jun 27 '18 at 19:03
  • If I would have to take a wild guess, I bet you were reading the data into R using something like `read.table()` that automatically reads colums as factors. If so, try to add `stringsAsFactors = FALSE` into your reading function. – Ni-Ar Jun 27 '18 at 22:56

0 Answers0