Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R I';我想在ggplot2的堆叠条形图上显示数据值_R_Ggplot2 - Fatal编程技术网

R I';我想在ggplot2的堆叠条形图上显示数据值

R I';我想在ggplot2的堆叠条形图上显示数据值,r,ggplot2,R,Ggplot2,我已成功创建堆叠条形图,但无法添加指示百分比的标签。这就是我所缺少的一切。 我基本上不知道如何正确使用geom_标签/geom_文本,我尝试了许多解决方案,但没有任何效果 我尝试过geom_文本函数,但它一直告诉我我做错了 一年一个月2个月,他供应军需汽油 2017年1月1日8 9408 SD2 15.38 126煤炭 2017年1月1日9 9388 SD3 15.46 218煤炭 2017年1月1日10 9393 SD3 15.46 218煤炭 2017年1月1日11 9628 SD4 15

我已成功创建堆叠条形图,但无法添加指示百分比的标签。这就是我所缺少的一切。 我基本上不知道如何正确使用geom_标签/geom_文本,我尝试了许多解决方案,但没有任何效果

我尝试过geom_文本函数,但它一直告诉我我做错了

一年一个月2个月,他供应军需汽油
2017年1月1日8 9408 SD2 15.38 126煤炭
2017年1月1日9 9388 SD3 15.46 218煤炭
2017年1月1日10 9393 SD3 15.46 218煤炭
2017年1月1日11 9628 SD4 15.47 203煤炭
2017年1月1日12 9943 EGC1 21.40 72天然气
2017年1月1日01 13 10106 BR5 21.41 245煤炭
2017年1月1日01 14 10114 BR5 21.41 245煤炭
2017年1月1日15 9971 EGC1 20.75 75天然气
2017年1月1日2016 10302 BR5 21.41 245煤炭
2017年1月1日1月17日10655 TC01 22.77 11天然气
2017年1月1日01 18 10811加州24.88 25天然气
2017年1月1日19 10821加州24.88 25天然气
2017年1月1日01月20日10765大26.00 30水电
2017年1月2日8 10428加州1 22.04 30天然气
2017年1月2日9 10723加州1 29.97 59天然气
2017年1月1日02 10 10933 BRA 44.50 30 HYDRO
2017年1月2日11 11107 ANC1 46.46 63天然气
2017年1月2日12 11098 ANC1 46.46 38天然气
2017年1月1日02 13 10839 JOF1 26.59 45天然气
2017年1月2日14 10814 JOF1 26.09 15天然气
2017年1月2日15 10797大26.00 30水电

sp我个人更喜欢使用
geom_col
而不是
geom_bar
,自己处理数据,而不是让ggplot2处理。这样你就可以更好地控制正在发生的事情

因为您没有提供所有数据,所以我只使用您提供的代码段

library(tibble)
MU17 <- tribble(~year, ~Month2, ~Month, ~Day, ~HE, ~Supply, ~MUnit, ~MPrice, ~MBlock, ~Fuel,
                    2017,    1,   "Jan",   01,    8,   9408,    "SD2",  15.38,  126,   "COAL",
                    2017,    1,   "Jan",   01,    9,   9388,    "SD3",  15.46,  218,  "COAL",
                    2017,    1,   "Jan",   01,    10,  9393,    "SD3",  15.46,  218,   "COAL",
                    2017,    1,   "Jan",   01,    11,  9628,    "SD4",  15.47,  203,   "COAL",
                    2017,    1,   "Jan",   01,    12,  9943,    "EGC1", 21.40,  72,    "GAS",
                    2017,    1,   "Jan",   01,    13,  10106,   "BR5",  21.41,  245,   "COAL",
                    2017,    1,   "Jan",   01,    14,  10114,   "BR5",  21.41,  245,   "COAL",
                    2017,    1,   "Jan",   01,    15,  9971,    "EGC1", 20.75,  75,    "GAS",
                    2017,    1,   "Jan",   01,    16,  10302,   "BR5",  21.41,  245,   "COAL",
                    2017,    1,   "Jan",   01,    17,  10655,   "TC01", 22.77,  11,    "GAS",
                    2017,    1,   "Jan",   01,    18,  10811,   "CAL1", 24.88,  25,    "GAS",
                    2017,    1,   "Jan",   01,    19,  10821,   "CAL1", 24.88,  25,    "GAS",
                    2017,    1,   "Jan",   01,    20,  10765,   "BIG",  26.00,  30,    "HYDRO",
                    2017,    1,   "Jan",   02,    8,   10428,   "CAL1", 22.04,  30,    "GAS",
                    2017,    1,   "Jan",   02,    9,   10723,   "CAL1", 29.97,  59,    "GAS",
                    2017,    1,   "Jan",   02,    10,  10933,   "BRA",  44.50,  30,    "HYDRO",
                    2017,    1,   "Jan",   02,    11,  11107,   "ANC1", 46.46,  63,    "GAS",
                    2017,    1,   "Jan",   02,    12,  11098,   "ANC1", 46.46,  38,    "GAS",
                    2017,    1,   "Jan",   02,    13,  10839,   "JOF1", 26.59,  45,    "GAS",
                    2017,    1,   "Jan",   02,    14,  10814,   "JOF1", 26.09,  15,    "HYDRO",
                    2017,    1,   "Jan",   02,    15,  10797,   "BIG",  26.00,  30,    "BIOMASS",

                    2017,    2,   "Feb",   01,    8,   9408,    "SD2",  15.38,  126,   "COAL",
                    2017,    2,   "Feb",   01,    9,   9388,    "SD3",  15.46,  218,  "COAL",
                    2017,    2,   "Feb",   01,    10,  9393,    "SD3",  15.46,  218,   "COAL",
                    2017,    2,   "Feb",   01,    11,  9628,    "SD4",  15.47,  203,   "COAL",
                    2017,    2,   "Feb",   01,    12,  9943,    "EGC1", 21.40,  72,    "GAS",
                    2017,    2,   "Feb",   01,    13,  10106,   "BR5",  21.41,  245,   "COAL",
                    2017,    2,   "Feb",   01,    14,  10114,   "BR5",  21.41,  245,   "COAL",
                    2017,    2,   "Feb",   01,    15,  9971,    "EGC1", 20.75,  75,    "GAS",
                    2017,    2,   "Feb",   01,    16,  10302,   "BR5",  21.41,  245,   "COAL",
                    2017,    2,   "Feb",   01,    17,  10655,   "TC01", 22.77,  11,    "GAS",
                    2017,    2,   "Feb",   01,    18,  10811,   "CAL1", 24.88,  25,    "GAS",
                    2017,    2,   "Feb",   01,    19,  10821,   "CAL1", 24.88,  25,    "GAS",
                    2017,    2,   "Feb",   01,    20,  10765,   "BIG",  26.00,  30,    "HYDRO",
                    2017,    2,   "Feb",   02,    8,   10428,   "CAL1", 22.04,  30,    "GAS",
                    2017,    2,   "Feb",   02,    9,   10723,   "CAL1", 29.97,  59,    "GAS",
                    2017,    2,   "Feb",   02,    10,  10933,   "BRA",  44.50,  30,    "HYDRO",
                    2017,    2,   "Feb",   02,    11,  11107,   "ANC1", 46.46,  63,    "GAS",
                    2017,    2,   "Feb",   02,    12,  11098,   "ANC1", 46.46,  38,    "GAS",
                    2017,    2,   "Feb",   02,    13,  10839,   "JOF1", 26.59,  45,    "GAS",
                    2017,    2,   "Feb",   02,    14,  10814,   "JOF1", 26.09,  15,    "HYDRO",
                    2017,    2,   "Feb",   02,    15,  10797,   "BIG",  26.00,  30,    "BIOMASS"
    )
我将这些数据导入ggplot。重要的是,我将
geom\u col
position=“fill”
一起使用。由于我为
geom_text
提供了一个position值
pos
,因此有必要在此处使用
position=“identity”
。此外,您需要某种
ifelse
-语句将
geom\u文本的
颜色调整为白色
\FFFFFF
,以便在
HYDRO
COAL
中使用较暗的背景色

祝您在原始数据上使用此方法好运

library(ggplot2)
library(dplyr)

MU17 %>%
    mutate(Fuel = factor(Fuel), 
           Month = factor(Month,levels = month.abb)) %>% 
    group_by(Month, Month2, Fuel) %>%
    summarise(n = n()) %>%
    group_by(Month) %>%
    mutate(p = n / sum(n),
           p2 = paste(formatC(p*100, digits = 2, format = "fg"),"%",sep = ""),
           pos = cumsum(p) - (0.5 * p)) %>%

    ggplot(aes(x = Month, y = p, fill = factor(Fuel, levels = rev(levels(Fuel))))) +   
    geom_col(width = 0.5, position = "fill") +
    scale_y_continuous(limits = c(0, 1), breaks = c(-.5,-.25,0,.25,.5,.75,1), expand = c(0, 0), 
                       labels = scales::percent) +
    scale_fill_manual(breaks = c("COAL", "GAS","HYDRO","BIOMASS"),
                      values = c("black","yellow","blue","green")) +
    geom_text(aes(label = p2, y = pos),
              position = "identity",
              vjust = 0.5,
              colour = ifelse(data$Fuel == "COAL" | data$Fuel == "HYDRO", "#FFFFFF", "#000000")) + 
    labs(x = "2017" , y = "Marginal Fuel Between HE8 & HE20") +
    labs(fill = "Fuel Type")

您需要共享一些数据。阅读这里了解如何:你在网站上试过答案了吗?如果其中一个不起作用,请将您尝试的代码与示例数据一起添加,这样人们就可以看到哪里出了问题。或者?你的问题看起来很像是重复的。@aosmith,是的,我已经试过了。基本上,我尝试过的每个解决方案都给了我相同的信息:错误:geom_文本需要以下缺失的美学:x,y。我只是试着教自己R,大部分情况下我都能独自解决问题,尽管我已经坚持了近两周。展示一次尝试是很有帮助的,这样我们就可以解决你到底做错了什么,而不仅仅是向你展示一个可行的解决方案。从您在评论中提到的错误消息来看,我认为问题在于您在
geom\u bar()
层中指定了
aes()
,因此它是该层的本地,而不是由geom文本层继承的。如果将
aes()
参数放在
ggplot()
中,而不是放在
geom\u bar()
中,则应该能够让其他解决方案工作。
library(ggplot2)
library(dplyr)

MU17 %>%
    mutate(Fuel = factor(Fuel), 
           Month = factor(Month,levels = month.abb)) %>% 
    group_by(Month, Month2, Fuel) %>%
    summarise(n = n()) %>%
    group_by(Month) %>%
    mutate(p = n / sum(n),
           p2 = paste(formatC(p*100, digits = 2, format = "fg"),"%",sep = ""),
           pos = cumsum(p) - (0.5 * p)) %>%

    ggplot(aes(x = Month, y = p, fill = factor(Fuel, levels = rev(levels(Fuel))))) +   
    geom_col(width = 0.5, position = "fill") +
    scale_y_continuous(limits = c(0, 1), breaks = c(-.5,-.25,0,.25,.5,.75,1), expand = c(0, 0), 
                       labels = scales::percent) +
    scale_fill_manual(breaks = c("COAL", "GAS","HYDRO","BIOMASS"),
                      values = c("black","yellow","blue","green")) +
    geom_text(aes(label = p2, y = pos),
              position = "identity",
              vjust = 0.5,
              colour = ifelse(data$Fuel == "COAL" | data$Fuel == "HYDRO", "#FFFFFF", "#000000")) + 
    labs(x = "2017" , y = "Marginal Fuel Between HE8 & HE20") +
    labs(fill = "Fuel Type")