scale_fill_distiller ggplot图例缺少某些数据框而不是其他数据框

时间:2017-10-14 12:38:23

标签: r ggplot2 legend

我是R的新手,所以我还没有想出如何对多个数据帧应用相同的更改并从每个结果数据帧中绘制图表。我通过在R markdown文件中逐个插入每个输入csv来完成此操作。

我在输入文件中使用相同的起始格式创建了5个数据框(此处仅显示3个),使用相同的操作集来获得" plottable"看起来像这样的数据框(这里只显示前几行)。只有" Stat"各种各样:

Df1-HAtype:

    Stat    Segment NullbyObs   id  stars
49  H4  HA  0.297083314 1   ***
25  H13 HA  0.09558552  2   ***
57  H5  HA  0.150072798 3   ***
9   H10 HA  0.184111297 4   ***
33  H2  HA  0.182582378 5   ***
81  H9  HA  0.341334025 6   ***
73  H7  HA  0.256536424 7   ***
65  H6  HA  0.260052443 8   ***
41  H3  HA  0.504939556 9   **
1   H1  HA  0.260205358 10  ***
17  H11 HA  0.503464461 11  **
56  H4  PB2 0.184828306 12  ***
32  H13 PB2 0.298094661 13  **
64  H5  PB2 0.199737211 14  ***

DF2-季节:

    Stat    Segment NullbyObs   id  stars
25  spring migration    HA  0.309504658 1   ***
17  overwintering   HA  0.423053354 2   *
9   breeding    HA  0.712614687 3   .
1   autumn migration    HA  0.586144734 4   .
32  spring migration    PB2 0.318294644 5   ***
24  overwintering   PB2 0.450508305 6   ***
16  breeding    PB2 0.636836392 7    

DF3-HOSTTYPE:

    Stat    Segment NullbyObs   id  stars
17  OD  HA  0.447867405 1   *
25  YAG HA  0.192755193 2   ***
9   MD  HA  0.337331563 3   **
1   BMG HA  0.42653249  4   *
24  OD  PB2 0.271704769 5   ***
32  YAG PB2 0.260490477 6   ***
16  MD  PB2 0.446735462 7   **

我使用此代码绘制每个数据框:

plot <- ggplot(data = plotdata,
       aes(x = Segment, y = Stat, fill = NullbyObs)) + 
       geom_tile() +
       scale_fill_distiller(palette = "RdYlBu", trans = "log10") +
       labs(y=NULL, x=NULL, fill="Null/Obs") +
       geom_text(aes(label=stars), color="black", size=4) +
       coord_equal()

plot

为什么Df1-HAtype的图表的图例显示但Df2-Season和Df3-HostType的图表中缺少?

1 个答案:

答案 0 :(得分:3)

scale_fill_distiller

中添加plotdata <- Df2_Season p <- ggplot(data = plotdata , aes(x = Segment, y = Stat, fill = NullbyObs)) + geom_tile() + scale_fill_distiller(palette = "RdYlBu", trans = "log10", breaks=seq(0,1,0.1), labels=seq(0,1,0.1)) + labs(y=NULL, x=NULL, fill="Null/Obs") + geom_text(aes(label=stars), color="black", size=4) + coord_equal() p
f(x,y,z)

<强> Df2_Season enter image description here

<强> Df3_HostType enter image description here