如何让情节从零开始?

时间:2017-08-30 23:21:22

标签: r ggplot2

我有一个数据框,我正在创建一个二维图,填充来自第三列。

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()

现在,Layer范围是1-10,但是当我绘制时,geom_tile从中点开始。如何从0开始,以10而不是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

2 个答案:

答案 0 :(得分:4)

geom_tile的默认图块高度为1,因此以Layer = 1为中心的图块将从0.5到1.5。最简单的解决方法是使用:ggplot(a, aes(Time, Layer-0.5, fill = Value)) + ...

答案 1 :(得分:3)

以下是:

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”校正值; “expand = c(0,0)”控制边距。

enter image description here