Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.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
ggplot2 geom_bar的问题_R_Ggplot2 - Fatal编程技术网

ggplot2 geom_bar的问题

ggplot2 geom_bar的问题,r,ggplot2,R,Ggplot2,我试图在y轴上使用ggplot2绘图百分比而不是频率,但它就是不起作用!我在绘图中添加了scale\u y\u continuous(labels=percent\u format()),但它仍然显示频率。这是我的密码: ggplot(items) + geom_bar( aes(x = type, fill = category), position = "dodge") + scale_y_continuous(labels = percent_format()) 这是我的数据集示例 he

我试图在y轴上使用ggplot2绘图百分比而不是频率,但它就是不起作用!我在绘图中添加了
scale\u y\u continuous(labels=percent\u format())
,但它仍然显示频率。这是我的密码:

ggplot(items) + geom_bar( aes(x = type, fill = category), position = "dodge") + scale_y_continuous(labels = percent_format())
这是我的数据集示例

head(items)

       item   type category
[1]    PA100   1    A
[2]    PB101   2    A
[3]    UR360   2    A
[4]    PX977   3    B
[5]    GA008   3    B
[6]    GR446   3    A
我要做的是,对于A类和B类,我要绘制类型1、类型2和类型3的百分比;因此我的代码。但不管它如何,都会在每个类别中绘制类型1、2和3的频率,而不是百分比:|

尝试:

items = structure(list(item = structure(c(3L, 4L, 6L, 5L, 1L, 2L), .Label = c("GA008", 
"GR446", "PA100", "PB101", "PX977", "UR360"), class = "factor"), 
    type = c(1L, 2L, 2L, 3L, 3L, 3L), category = structure(c(1L, 
    1L, 1L, 2L, 2L, 1L), .Label = c("A", "B"), class = "factor")), .Names = c("item", 
"type", "category"), class = "data.frame", row.names = c(NA, 
-6L))

items
   item type category
1 PA100    1        A
2 PB101    2        A
3 UR360    2        A
4 PX977    3        B
5 GA008    3        B
6 GR446    3        A

tt = with(items, table(type, category))
tt
    category
type A B
   1 1 0
   2 2 0
   3 1 2

dd = data.frame(prop.table(tt, 2))
dd
  type category Freq
1    1        A 0.25
2    2        A 0.50
3    3        A 0.25
4    1        B 0.00
5    2        B 0.00
6    3        B 1.00

ggplot(dd)+geom_bar(aes(x=type, y=Freq, fill=category), stat='identity', position='dodge')

你的目标是什么?已经有百分比了吗?如果您可以添加可复制的数据,那就太好了。@jazzurro errr我没有自己的y,我在x轴上有一个分类变量,我想在y轴上绘制它的百分比。让我在我的报告中再补充一些细节question@user20650感谢y轴的比例已更改,但绘图保持不变:((@user20650不,我的意思是这些图仍然是频率级别,而不是类别级别。我知道,因为我计算了每个类别中每种类型的百分比,而这不是这些图所显示的。除了y轴上的缩放比例仅显示0%到25%之外,我不知道发生了什么,请参见下面我的答案。这是你想要的吗?