如何使用ggplot2填充轮廓图?

时间:2019-02-04 22:34:54

标签: r ggplot2 contour

我有以下代码创建轮廓图:

library(ggplot2)
library(tidyverse)

vector1 <- c(0,1,4,9,11,18)
vector2 <- c(1,5,7,9,14,16,17,20,40)
theme_bw()
result <- matrix(0, nrow = length(vector1), ncol = length(vector2))
for (i in seq_len(length(vector1))) {
  for (j in seq_len(length(vector2))) {
    result[i,j] <- 1 + (vector2[j] * (vector1[i]))
  }
}
rownames(result) <- vector1
colnames(result) <- vector2
as.data.frame(result) %>%
  rownames_to_column() %>% 
  gather(key, value, -rowname) %>%
  mutate(key = as.numeric(key), 
         rowname = as.numeric(rowname)) %>%

  ggplot() +
  geom_contour(aes(x = rowname, y = key, z = value))

This is the result

但是我想要这样的东西:

enter image description here

如何填充R中的等高线图,以得到像彩色示例一样的图?

0 个答案:

没有答案