I have a question regarding a weird result for a line plot that uses facets. I have water data masurements for different depth (=pressures). The data comes as a table as such:
Pressure Temperature pH
0 30 8.1
1 28 8.0
I "melt" this data to yield:
Pressure variable value
0 Temperature 30
1 Temperature 30
0 pH 8.1
1 pH 8.0
and so on. I now plot this:
ggplot(data.m.df, aes(x=value, y=Pressure)) +
facet_grid(.~variable, scale = "free") +
scale_y_reverse() +
geom_line() +
opts(axis.title.x=theme_blank())
It kinda works, except there are parts of the line plot that get filled with solid color. I have no idea why, especially because it works just fine if I exchange x for y and use "variable ~ ." as the facet_grid formula.
17.1 Facet wrap This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. You can control how the ribbon is wrapped into a grid with ncol , nrow , as. table and dir . ncol and nrow control how many columns and rows (you only need to set one).
The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.
aes() is an argument within ggplot that takes its own arguments, aes(x=, y=) . These are your independent (x) variable and your dependent (y) variable.
Note the difference between geom_line
and geom_path
applied to the same data.
library(ggplot2)
x = c(seq(1, 10, 1), seq(10, 1, -1))
y = seq(0, 19, 1)
df = data.frame(x, y)
ggplot(df, aes(x, y)) + geom_line()
ggplot(df, aes(x, y)) + geom_path()
Note the order in the df
data frame.
x y
1 1 0
2 2 1
3 3 2
4 4 3
5 5 4
6 6 5
7 7 6
8 8 7
9 9 8
10 10 9
11 10 10
12 9 11
13 8 12
14 7 13
15 6 14
16 5 15
17 4 16
18 3 17
19 2 18
20 1 19
geom_path
plots in order of the observations.
geom_line
plots in order of x values.
The effect is more marked when the x values are closer together.
x = c(seq(1, 10, .01), seq(10, 1, -.01))
y = seq(.99, 19, .01)
df = data.frame(x, y)
ggplot(df, aes(x, y)) + geom_line()
ggplot(df, aes(x, y)) + geom_path()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With