Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中具有值和一些附加功能的热图_R_Dataframe_Ggplot2_Heatmap_Fill - Fatal编程技术网

在R中具有值和一些附加功能的热图

在R中具有值和一些附加功能的热图,r,dataframe,ggplot2,heatmap,fill,R,Dataframe,Ggplot2,Heatmap,Fill,我试图在R中制作一个热图,它与填充一起也应该显示值,与填充一起,还应该包括一行和一列,而不在其上应用填充 我的数据是这样的 Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Sum 1999 116 127 202 229 253 271 271 245 212 174 133 115 2348 2000 114 154 193 214 263 271 254 248 210 167 120 102 2310 2001

我试图在R中制作一个热图,它与填充一起也应该显示值,与填充一起,还应该包括一行和一列,而不在其上应用填充

我的数据是这样的

Year    Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Sum
1999    116 127 202 229 253 271 271 245 212 174 133 115 2348
2000    114 154 193 214 263 271 254 248 210 167 120 102 2310
2001    124 134 194 202 256 270 270 243 212 176 128 108 2317
2002    107 137 199 213 267 269 267 250 211 153 123 109 2304
2003    116 141 192 220 229 265 273 242 215 171 122 107 2294
2004    116 146 191 220 245 268 269 253 212 158 115 107 2301
2005    117 132 191 205 263 261 269 244 211 176 132 110 2310
2006    117 125 203 215 246 263 269 248 207 171 128 116 2309
2007    127 133 186 205 244 268 272 251 210 170 133 118 2317
2008    113 146 201 224 259 266 271 245 201 165 128 114 2335
2009    128 145 201 211 243 264 271 247 211 165 126 111 2324
2010    125 128 188 220 254 263 255 234 199 170 127 112 2277
2011    120 131 203 217 241 268 263 247 205 173 131 113 2312
2012    119 133 195 220 241 263 266 249 204 156 120 113 2278
2013    117 142 202 224 248 262 270 249 209 177 117 101 2317
2014    118 144 181 212 233 258 273 250 209 168 126 111 2284
2015    110 132 189 228 257 262 269 241 194 155 113 111 2260
2016    117 147 184 217 253 259 269 238 211 170 123 112 2299
2017    121 147 196 219 260 266 256 242 206 176 132 110 2332
Average 118 138 194 217 250 265 267 246 208 168 125 111 2307
我的数据不仅代表每月的数值,还考虑了每月的总和和平均值。仅使用月值制作热图并不困难,我已经做到了

我的作品链接在这里:

我不想在热图中包含表示总和和平均值的最后一行和最后一列。我不知道该怎么做。请带我到这里。我想使用ggplot2或热图软件包执行此任务


我想要这样的东西:

我们只需要将数据重新排列为长格式。然后进行绘图,确保我们只向
geom\u tile
提供数据的子集。然后我们重新排列轴的顺序

library(ggplot2)

dat2 <- stack(dat[-1])
dat2$year <- dat$Year

ggplot(mapping = aes(ind, year)) +
  geom_tile(aes(fill = values), subset(dat2, year != "Average" & ind != "Sum")) +
  geom_text(aes(label = round(values, 1)), dat2) +
  scale_y_discrete(limits = c("Average", 2017:1999)) +
  scale_x_discrete(limits = c(month.abb, "Sum"), position = "top") +
  viridis::scale_fill_viridis() +
  theme_minimal() + theme(axis.title = element_blank())
库(ggplot2)

dat2我们只需要将数据重新排列为长格式。然后进行绘图,确保我们只向
geom\u tile
提供数据的子集。然后我们重新排列轴的顺序

library(ggplot2)

dat2 <- stack(dat[-1])
dat2$year <- dat$Year

ggplot(mapping = aes(ind, year)) +
  geom_tile(aes(fill = values), subset(dat2, year != "Average" & ind != "Sum")) +
  geom_text(aes(label = round(values, 1)), dat2) +
  scale_y_discrete(limits = c("Average", 2017:1999)) +
  scale_x_discrete(limits = c(month.abb, "Sum"), position = "top") +
  viridis::scale_fill_viridis() +
  theme_minimal() + theme(axis.title = element_blank())
库(ggplot2)

太棒了!感谢洛蒂夫,我想用ggtext向这个图中添加文本,并且必须在文本中提到单位。假设我想写->GHI(kWh/m^2)。我怎样才能把它包括进去,这样我就可以在单元和文本之间留有空间,而且单元中的权力应该以适当的方式书写。这是一个完全独立的问题。举一个简单的例子(你不需要整个热图),然后作为一个新问题问它。我想出来了。所以现在不需要提问了。谢谢!精彩的!感谢洛蒂夫,我想用ggtext向这个图中添加文本,并且必须在文本中提到单位。假设我想写->GHI(kWh/m^2)。我怎样才能把它包括进去,这样我就可以在单元和文本之间留有空间,而且单元中的权力应该以适当的方式书写。这是一个完全独立的问题。举一个简单的例子(你不需要整个热图),然后作为一个新问题问它。我想出来了。所以现在不需要提问了。谢谢!请不要转发问题。请不要转发问题。