Reputation: 319
I'm in ggplot2 applying scale_fill_gradient2
on single variable. geom_histogram()
is my default approach, but I'd like to use geom_dotplot()
. When I widen my x-axis, the fill on the histogram maps to my axis limits (which is desired), but when plotting the same data using dotplot, the fill maps to the min/max of the data regardless of the x-axis limits. Sample Code:
### Use the Iris Data
df <- iris
### Basic ggplot setup
p <- ggplot(df, aes(x=Sepal.Length))
## Histogram -- fill scales with my axis limits (desired)
p + geom_histogram(color="black",
aes(fill = ..x..)) +
scale_x_continuous(limits=c(2,10))+
scale_fill_gradient2(
low = "blue", high = "red",
mid = "white", midpoint=6)
In other words, the fill scale matches the scale_x_continuous command and produces the fill result I desire. However, change the word "histogram" to "dotplot" (and nothing else), and the fill scale sticks purely with the min/max of the data. What I want is the dotplot with the histogram fill.
### Dot Plot -- fill scales purely with min/max of data
### I want the same shading as in the histogram
p + geom_dotplot(color="black",
aes(fill = ..x..)) +
scale_x_continuous(limits=c(2,10))+
scale_fill_gradient2(
low = "blue", high = "red",
mid = "white", midpoint=6
Ultimately, the goal here is to have the dotplot fill/scale match that of the histogram. Thanks!
Upvotes: 2
Views: 905
Reputation: 174278
You can set the limits
in scale_fill_gradient2
:
p + geom_dotplot(color="black",
aes(fill = ..x..)) +
scale_x_continuous(limits = c(2, 10))+
scale_fill_gradient2(
low = "blue", high = "red",
mid = "white", midpoint = 6,
limits = c(2, 10))
Upvotes: 2