I'm an R newbie and I'm probably missing something trivial, but here it goes:
I have a data frame Data
with values like so:
Voltage Current lnI VoltageRange
1 0.474 0.001 -6.907755 Low Voltage
2 0.883 0.002 -6.214608 Low Voltage
3 1.280 0.005 -5.298317 Low Voltage
. . . . .
. . . . .
. . . . .
. . . . .
13 2.210 0.247 -1.398367 High Voltage
Then I try to plot it with the following code:
ggplot(data = Data, mapping = aes(x = Data$lnI, y = Data$Voltage)) +
geom_point() +
stat_smooth(method = "lm", se = FALSE) +
facet_grid(~VoltageRange)
As you can see, the facet labels are in the wrong place, what is labeled as High voltage corresponds to low voltage and vice versa.
How do I go about fixing this? What am I doing wrong?
As commented. I think your ggplot call is 'too complicated'
require(read.so) #awesome package available on GitHub, by @alistaire47
dat <- read_so()
dat <- dat[c(1:3,8),]
dat
# A tibble: 4 x 4
Voltage Current lnI VoltageRange
<chr> <chr> <chr> <chr>
1 0.474 0.001 -6.907755 Low
2 0.883 0.002 -6.214608 Low
3 1.280 0.005 -5.298317 Low
4 2.210 0.247 -1.398367 High
ggplot(dat, aes(x = lnI, y = Voltage)) + # remove 'mapping',
# and use only the object names, not the columns/ vectors
geom_point() +
stat_smooth(method = "lm", se = FALSE) +
facet_grid(~VoltageRange)
edit If you want to re-arrange the facet, factorise the argument and change the order of the levels. You can do this in the data frame (which I would not recommend) or in the ggplot call directly. In order to do so, I find it good to create a character vector with the order of the levels, because you might need this one again.
facet_order <- c('Low', 'High')
# note it's important that the levels are written exactly the same
ggplot(dat, aes(x = lnI, y = Voltage)) +
stat_smooth(method = "lm", se = FALSE) +
facet_grid(~factor(VoltageRange, levels = facet_order))