I'm trying to make a contour plot in ggplot using segmented data, where one block has a spacing between the X-axis elements of 2, while the next block has a spacing between the X-axis elements of 2.5. This causes ggplot to show a homogeneous carpet in the plot only in the first segment. Somehow it gets "fixed" in that segment and when it starts to plot the other one it shows empty spaces.
The dataframe look like:
> head(df_example,3)
temp1 temp2 value
1 235.99 0.00 151069
2 235.99 0.01 151850
3 235.99 0.02 146958
Verify the gaps:
> unique(df_example$temp1)
[1] 235.99 237.99 239.99 241.99 243.99 245.99 247.99 249.99 252.49 254.99 257.49 259.99 262.49 264.99
> diff(unique(df_example$temp1))
[1] 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.5 2.5 2.5 2.5 2.5 2.5
We can see that before 249.99 we have a difference of 2 between the elements, but afterwards this difference is 2.5. Any idea how to plot ignoring different spacing between elements?
> dput(df_example)
structure(list(temp1 = c(235.99, 235.99, 237.99, 237.99, 239.99,
239.99, 241.99, 241.99, 243.99, 243.99, 245.99, 245.99, 247.99,
247.99, 249.99, 249.99, 252.49, 252.49, 254.99, 254.99, 257.49,
257.49, 259.99, 259.99, 262.49, 262.49, 264.99, 264.99, 252.49,
252.49, 252.49, 254.99, 254.99, 254.99, 257.49, 257.49, 257.49,
259.99, 259.99, 259.99, 262.49, 262.49, 262.49, 264.99, 264.99,
264.99), temp2 = c(0, 0.01, 0, 0.01, 0, 0.01, 0, 0.01, 0, 0.01,
0, 0.01, 0, 0.01, 0, 0.01, 0, 0.00999999999999091, 0, 0.00999999999999091,
0, 0.00999999999999091, 0, 0.00999999999999091, 0, 0.00999999999999091,
0, 0.00999999999999091, 0, 0.00999999999999091, 0.0200000000000102,
0, 0.00999999999999091, 0.0200000000000102, 0, 0.00999999999999091,
0.0200000000000102, 0, 0.00999999999999091, 0.0200000000000102,
0, 0.00999999999999091, 0.0200000000000102, 0, 0.00999999999999091,
0.0200000000000102), value = c(151069, 151850, 150899, 155987,
148109, 155443, 153611, 159438, 153413, 158505, 144172, 147911,
146392, 147019, 150317, 146777, 152075, 145706, 153543, 143011,
149350, 140979, 151160, 143074, 155398, 148090, 149558, 147063,
152075, 145706, 153090, 153543, 143011, 151019, 149350, 140979,
150483, 151160, 143074, 143946, 155398, 148090, 153473, 149558,
147063, 145317)), row.names = c(NA, -46L), class = "data.frame")
I tried:
ggplot2::ggplot(df_example, ggplot2::aes(x=temp1, y=temp2, fill=value)) +
ggplot2::geom_tile() +
ggplot2::scale_fill_viridis_c() +
ggplot2::labs(x='temp 1',
y= 'temp 2', fill= "value")+
ggplot2::theme(plot.title=ggplot2::element_text(hjust=0.5))+
ggplot2::scale_x_continuous(expand = c(0, 0)) +
ggplot2::scale_y_continuous(expand = c(0, 0)) +
ggplot2::guides(fill = ggplot2::guide_colorbar(ticks = FALSE))
Simply setting the width
in geom_tile
to the larger "gap" will take care of it. Here, I am also setting the color = "white"
for demonstration purposes so each tile is clearly outlined.
ggplot(df_example, aes(x=temp1, y=temp2, fill=value)) +
geom_tile(width = 2.5, color = "white") +
scale_fill_viridis_c() +
labs(x='temp 1',y= 'temp 2', fill= "value")+
theme(plot.title=element_text(hjust=0.5))+
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
guides(fill = guide_colorbar(ticks = FALSE))