maximusdooku
maximusdooku

Reputation: 5512

How can I make the plot start at zero?

I have a dataframe from which I am creating a 2-D plot where fill is from a third column.

ggplot(a, aes(x=time, y=Layer, fill = Value))+
  geom_tile()+
  scale_fill_gradient2(low="blue",mid="yellow",high="red", midpoint=0.5)+
  theme_bw()

Now, Layer range is 1-10, but when I plot, the geom_tile starts from midpoint. How can I make it start at 0 and end at 10 instead of 10.5?

dput(a)

structure(list(Layer = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L), mLayerVolFracLiq = c(0.363718066074495, 0.363933384428794, 
0.364400746014017, 0.365175162266439, 0.366478613460277, 0.367862103805244, 
0.369580612135753, 0.371329557433169, 0.272431415602475, 0.327468643048551, 
0.327897896730601, 0.32856502757375, 0.329595993919462, 0.330599478658051, 
0.331763769119895, 0.332889478379646, 0.239569037788007, 0.317540647774584, 
0.322494562891295, 0.323358879198933, 0.324561654946114, 0.325586756470287, 
0.326653284285629, 0.327532833630272, 0.261175199102923, 0.301400809191216, 
0.321074149620943, 0.321607194199411, 0.322388735750387, 0.323056962371599, 
0.323757666857778, 0.32442138598775, 0.312212741188763, 0.324776654481821, 
0.324768151152297, 0.324756637490834, 0.324726976153914, 0.324618664763349, 
0.32433134053386, 0.323817044326496, 0.251971636137293, 0.320441574804528, 
0.321595054735562, 0.32290107949736, 0.324310319642686, 0.325122235790138, 
0.325603020063082, 0.325372562796669, 1.73807585576703e-07, 1.01045675059099e-06, 
0.254757797872636, 0.268074882490628, 0.319964072649761, 0.32034002493773, 
0.320900756641409, 0.321453528320185, 0.322177257826123, 0.323115274003383, 
1.24486825410929e-07, 6.25566160415274e-07, 0.242701604730622, 
0.259436929276621, 0.317826642868764, 0.318457767194441, 0.319300660275628, 
0.320020661370089, 0.320835746922608, 0.32167722625929, 1.87615579711712e-07, 
7.44971438115926e-07, 0.237228459751742, 0.249074201814969, 0.315716905648282, 
0.31707145209583, 0.31798052983471, 0.318739416082674, 0.319620457203197, 
0.320534849645649, 6.89365505790602e-07, 2.05727270325164e-06, 
0.241335088464608, 0.25061601558339, 0.310690297750807, 0.316451528382204, 
0.317123994951979, 0.317784575834729, 0.318651454537485, 0.319589121434301
), time = structure(c(17146, 17146, 17146, 17146, 17146, 17146, 
17146, 17146, 17147, 17147, 17147, 17147, 17147, 17147, 17147, 
17147, 17148, 17148, 17148, 17148, 17148, 17148, 17148, 17148, 
17149, 17149, 17149, 17149, 17149, 17149, 17149, 17149, 17150, 
17150, 17150, 17150, 17150, 17150, 17150, 17150, 17151, 17151, 
17151, 17151, 17151, 17151, 17151, 17151, 17152, 17152, 17152, 
17152, 17152, 17152, 17152, 17152, 17152, 17152, 17153, 17153, 
17153, 17153, 17153, 17153, 17153, 17153, 17153, 17153, 17154, 
17154, 17154, 17154, 17154, 17154, 17154, 17154, 17154, 17154, 
17155, 17155, 17155, 17155, 17155, 17155, 17155, 17155, 17155, 
17155), class = "Date")), row.names = c(NA, -88L), .Names = c("Layer", 
"Value", "time"), class = "data.frame")

enter image description here

Upvotes: 1

Views: 362

Answers (2)

www
www

Reputation: 4224

Here's how:

ggplot(a, aes(x=time, y=Layer-0.5, fill = Value))+
  geom_tile()+
  scale_y_continuous(expand = c(0,0))+
  scale_fill_gradient2(low="blue",mid="yellow",high="red", midpoint=0.5)+
  theme_bw()

"Layer-0.5" corrects the values; "expand=c(0, 0)" controls margins.

enter image description here

Upvotes: 4

Brian
Brian

Reputation: 8275

The default tile height for geom_tile is 1, so a tile centered on Layer = 1 will go from 0.5 to 1.5. The simplest workaround is to use: ggplot(a, aes(Time, Layer-0.5, fill = Value)) + ...

Upvotes: 3

Related Questions