使用“fill”在 ggplot 中创建颜色渐变

问题描述 投票:0回答:1

我面临以下问题: 我正在尝试使用 ggplot2 为我的数据创建热图,一切工作正常并且看起来不错,除了在我的图例中,我的颜色被分为与“syn_count”列相对应的单独阶段。现在我想知道是否有什么方法可以在图例中将不同的阶段表示为渐变。

这是我的代码:

all <- ggplot(Neu, aes(type, tag, fill = syn_count))+
  geom_tile()+
  theme(text = element_text(size=8),axis.text.x = element_text(angle = 1, hjust = 1))+
  coord_fixed()+
  scale_fill_distiller(palette="YlGn")+
  labs(title="Inputs of All MeMe-Neurons", x="MeMe-Neuron", y="Inputneuron")+
  guides(fill=guide_legend(title="Number of Synapses"))
all

以及相应的我的情节: My Plot

我浏览了这里并看到了库比例和scale_fill_gradient2的解决方案。但由于我在加载库时遇到一些问题,我想知道是否有任何方法可以使用 ggplot2 包而不添加任何其他包。

我希望这里有人能帮助我并提前致谢!

编辑: 这是一个示例数据框:

Tag/syn_count/type
A   24         1
E   500        1
F   310        2
F   67         2
G   266        3
T   80         3
H   270        4

标签(字符) 类型(字符) syn_count(num)

r ggplot2 heatmap
1个回答
0
投票

问题出在

guides(fill=guide_legend(title="Number of Synapses"))
,你应该在
lab()
中插入填充的标题。

这是一个例子:

df = tibble(tag = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")[runif(100, 1, 10) %>% round()],
            syn_count = runif(100, 1, 500) %>% round(),
            type = runif(100, 1, 10) %>% round())

ggplot(df, aes(type, tag, fill = syn_count))+
  geom_tile()+
  theme(text = element_text(size=8),axis.text.x = element_text(angle = 1, hjust = 1))+
  coord_fixed()+
  scale_fill_distiller(palette="YlGn")+
  # add legend title 
  labs(title="Inputs of All MeMe-Neurons",
       x="MeMe-Neuron",
       y="Inputneuron",
       fill="Number of Synapses")   # HERE your legend's title

© www.soinside.com 2019 - 2024. All rights reserved.