2014-02-27 26 views
0

我有两个dataframes,并绘制出第一个饼图的琢面网格工作正常:文本颜色不匹配传说中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 chart by converting to polar coord 
    scale_x_discrete("") +    # no x-axis label 
    scale_y_continuous("") 

Just one dataframe without labels

my_dat$variable是列的因素并决定颜色,网格将Cation作为行,Anion作为列。此图有一个图例,当我尝试使用geom_text添加文本标签时,问题就开始了。我在想中心这些数字,

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) 

本数据来源于称为tot.E.dat另据帧,基本上我想在每个面的中心的数量。结果是这样的: Added the label data frame

正如你所看到的,它可以做我想做的,但是有一点点呃逆。我不介意馅饼已经变成了甜甜圈,但我确实介意这个传说现在有一个“Total_EFP_Energy”,并且与文字颜色不匹配。我如何获得数字的颜色以匹配图例?如果没有,我怎样才能摆脱传说中的那个入口?

谢谢。

这里有一些数据(我不认为你想要所有的行)。第一个是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 
+0

你能提供一个可重复的例子(例如 - 一些数据!)。 – mnel

+1

我认为这是因为数据之间通常使用“变量” - 尝试将其更改为“tot.E.dat”中的其他内容。 – alexwhan

+0

嗯...我想你是在正确的轨道上。我做了名字(tot.E.dat)< - c(“Cation”,“Anion”,“Energy”,“value”)',然后抱怨找不到'variable'。 –

回答

0

这是太长岗位作为清晰的注释。

你可以尝试这样的东西 -

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) 

的想法是,与其在ggplot呼叫,然后应用到整个车指定美学,我现在在geom指定它,所以它应该只适用于那个几何。 fill = variable部分不适用于geom_text,现在不应显示在图例中。

+0

这个伎俩!我可以生活在没有总能量出现在传说中,我只需要记住解释那个数字意味着什么地方。谢谢!小评论,它应该是'geom_bar(data = my_dat,aes(....' –

+0

更好的解决方案,在我看来,将重命名'tot.E.dat $变量'为其他东西,合并这两个数据集在'c'('cation','anion')',然后'ggplot'上只有一个数据集。这应该可以解决定位问题和你的传奇问题。 – TheComeOnMan

+0

这是一个好主意,我会稍后再尝试。 –