R 文字颜色与ggplot2中使用geom_文字的图例不匹配

R 文字颜色与ggplot2中使用geom_文字的图例不匹配,r,ggplot2,R,Ggplot2,我有两个数据帧,将第一个数据帧绘制为分面饼图网格很好: pie_grid <- ggplot(my_dat, aes(x = factor(""), y = value, fill = variable)) + geom_bar(stat = "identity") + # bar plot facet_grid(Cation ~ Anion) + # grid coord_polar(theta = "y") + # pie

我有两个数据帧,将第一个数据帧绘制为分面饼图网格很好:

pie_grid <- ggplot(my_dat, aes(x = factor(""), y = value, fill = variable)) + 
   geom_bar(stat = "identity") +       # bar plot
   facet_grid(Cation ~ Anion) +        # grid
   coord_polar(theta = "y") +          # pie chart by converting to polar coord
   scale_x_discrete("") +              # no x-axis label
   scale_y_continuous("")
数据来自另一个名为
tot.E.dat
的数据帧,基本上我希望在每个方面的中心都有一个数字。结果如下所示:

正如你所看到的,这是我想要的,但有一个轻微的打嗝。我不介意馅饼变成了甜甜圈,但我确实介意传说现在有一个“总能量”,而且颜色与文字不匹配。如何获得与图例匹配的数字颜色?如果没有,我怎样才能删除图例中的条目

多谢各位

下面是一些数据(我认为您不需要所有的行)。第一个是
my_dat

   Cation  Anion        variable        value
1     im    bf4    Electrostatic -388.8225640
2     im     br    Electrostatic -440.0319478
3     im     cl    Electrostatic -462.6507643
4     im    dca    Electrostatic -387.0396472
5     im    mes    Electrostatic -434.2880350
...
17    im    bf4        Repulsion   79.0755418
18    im     br        Repulsion  180.1054541
19    im     cl        Repulsion  181.7249981
20    im    dca        Repulsion  105.0390379
21    im    mes        Repulsion  112.6103998
...
74   pyr     br  Charge_Transfer    0.5827333
75   pyr     cl  Charge_Transfer    0.3390909
76   pyr    dca  Charge_Transfer   -0.9314203
77   pyr    mes  Charge_Transfer   -2.2744731
78   pyr   ntf2  Charge_Transfer   -1.0463488
79   pyr    pf6  Charge_Transfer   -1.6858646
80   pyr    tos  Charge_Transfer   -2.0899762
并且也
tot.E.dat

   Cation  Anion         variable     value
1     im    bf4  Total_EFP_Energy -405.5935
2     im     br  Total_EFP_Energy -382.3632
3     im     cl  Total_EFP_Energy -407.9164
4     im    dca  Total_EFP_Energy -399.4065
5     im    mes  Total_EFP_Energy -463.8081
6     im   ntf2  Total_EFP_Energy -380.7301
7     im    pf6  Total_EFP_Energy -376.5059
8     im    tos  Total_EFP_Energy -498.7680
9    pyr    bf4  Total_EFP_Energy -375.3014
10   pyr     br  Total_EFP_Energy -373.2262
11   pyr     cl  Total_EFP_Energy -404.0563
12   pyr    dca  Total_EFP_Energy -366.3844
13   pyr    mes  Total_EFP_Energy -428.7498
14   pyr   ntf2  Total_EFP_Energy -341.2173
15   pyr    pf6  Total_EFP_Energy -353.0248
16   pyr    tos  Total_EFP_Energy -419.7708

这太长,无法作为清晰的评论发布

你能试试这样的吗-

ggplot() + 
geom_bar(my_dat, aes(x = factor(""), y = value, fill = variable), stat = "identity") +
geom_text( aes(x = 0, y = 0, size = 4,
               label = formatC(round(value, digits =2), format = 'f', digits = 2)), 
               data = tot.E.dat,
               show_guide = FALSE,
               hjust = 0, vjust = 1)

其想法是,我现在在
geom
中指定美学,而不是在
ggplot
调用中指定美学,然后将其应用于整个购物车,因此它应该只应用于该geom。
fill=variable
部分不应应用于
geom_文本
,现在不应显示在图例上。

您能提供一个可复制的示例(例如-一些数据!).我认为这是因为
变量
在数据之间很常见-尝试在
tot.E.dat
中将其更改为其他内容。嗯……我认为您的思路是正确的。我做了
name(tot.E.dat)这件事!没有传说中出现的总能量,我可以活下去,我只需要记住在其他地方解释这个数字的含义。谢谢小评论,应该是
geom\u bar(data=my\u dat,aes(…
在我看来,一个更好的解决方案是将
tot.E.dat$variable
重命名为其他名称,将这两个数据集合并到
c('阳离子','阴离子'))
,然后只在一个数据集上绘制
ggplot
。这也会解决定位问题和图例的问题。
ggplot() + 
geom_bar(my_dat, aes(x = factor(""), y = value, fill = variable), stat = "identity") +
geom_text( aes(x = 0, y = 0, size = 4,
               label = formatC(round(value, digits =2), format = 'f', digits = 2)), 
               data = tot.E.dat,
               show_guide = FALSE,
               hjust = 0, vjust = 1)