1

I have a dataframe of reflectance data that I have melted to more easily use ggplot, here's the first few rows of the dataframe bronmelt:

   variety  wavelength   reflectance
1 magnolia  wavel.400    1.602868
2 carlos    wavel.450    1.778760
3 scupper   wavel.500    1.352016
4 magnolia  wavel.600    5.969302
5 scupper   wavel.900    1.491008

My problem is that when I call a simple plot:

ggplot(data=bronmelt, aes(x=wavelength, y=reflectance, color = variety)) + geom_point()

to plot the data, I can't get the x axis to be seen as a continuous variable.

How do I create a custom x axis from 400-900 that has tick marks every 20 points?

camdenl
  • 1,159
  • 4
  • 21
  • 32
  • Can you please post a [minimal, reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). None of the variables in your `ggplot` call are represented in your sample data. Thanks. – Henrik Dec 05 '13 at 20:09
  • Edited plot call to reflect variable names – camdenl Dec 05 '13 at 20:20

1 Answers1

4

First create a new column with numeric wavelengh values:

bronmelt <- transform(bronmelt, 
                      wavelength2 = as.integer(substr(wavelength, 7, 10)))

The plot:

library(ggplot2)
ggplot(data=bronmelt, aes(x=wavelength2, y=reflectance, color = variety)) + 
  geom_point() +
  scale_x_continuous(breaks = seq(400, 900, 20))

The last line specifies the axis breaks ranging from 400 to 900 in steps of 20.

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • 1
    +1 Good answer! I'm curious - why did you use `transform` instead of something like `bronmelt$wavelength2 <- as.integer(substr(bronmelt$wavelength, 7, 10))`? Just wondering if there are any specific advantages, or if it's just a stylistic choice. Thanks! – Matt Parker Dec 05 '13 at 20:32
  • 1
    @MattParker It's more a stylistic choice. But the advantage is that `transform` allows to create multiple variables at once. – Sven Hohenstein Dec 05 '13 at 20:39