简体   繁体   中英

Text colour does not match legend using geom_text in ggplot2

I have two dataframes, and plotting the first one as a facetted grid of pie charts works fine:

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("")

仅有一个没有标签的数据框

Where my_dat$variable is a column of factors and decides the colour, the grid has Cation as the rows and Anion as the columns. This graph has a legend that The problem begins when I try to add in text labels using geom_text . I want these numbers in the centre,

pie_grid <- pie_grid + 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)

The data comes from another dataframe called tot.E.dat , and basically I want a number in the centre of each facet. The result looks like this: 添加了标签数据框

As you can see, it does what I want, but there is a slight hiccup. I don't mind that the pies have become donuts, but I do mind that the legend now has a "Total_EFP_Energy", and that doesn't match in colour with the text. How can I get the colour of the numbers to match the legend? If not, how can I get rid of that entry in the legend?

Thank you.

Here is some data (I don't think you want all the rows). The first one is 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

And also 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

This is too long to post as a legible comment.

Can you try something like this -

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)

The idea being that instead of specifying the aesthetics in the ggplot call, which then applies to the whole cart, I am now specifying it in the geom , so it should apply only to that geom. The fill = variable part should not apply to the geom_text and shouldn't show up on the legend now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM