Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.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 自动选择使条形图显示其结果_R_Ggplot2 - Fatal编程技术网

R 自动选择使条形图显示其结果

R 自动选择使条形图显示其结果,r,ggplot2,R,Ggplot2,我有一个堆叠的条形图,如下所示: library(ggplot2) Year <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4)) Category <- c(rep(c("A", "B", "C", "D"), times = 4))

我有一个堆叠的条形图,如下所示:

library(ggplot2)
Year      <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))
Category  <- c(rep(c("A", "B", "C", "D"), times = 4))
Frequency <- c(1, 1, 8, 32, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)
Data      <- data.frame(Year, Category, Frequency)
ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency)) +
    geom_bar(stat = "identity") +
    geom_text(size = 3, position = position_stack(vjust = 0.5))
库(ggplot2)

年份在绘图上整齐地显示这一点没有理想的解决方案。您可以使用
ggrepel
中的
geom\u label\u repel

library(ggplot2)
library(ggrepel)

ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency)) +
    geom_bar(stat = "identity") +
    geom_label_repel(size = 3, position = position_stack(vjust = 0.5))

或具有自由刻度的镶嵌面:

ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency)) +
    geom_bar(stat = "identity") +
    geom_text(size = 3, position = position_stack(vjust = 0.5)) +
  facet_wrap(.~Year, drop = TRUE, nrow = 1, scales = "free") +
  theme(strip.background = element_blank(), strip.text = element_blank())

或者是来自
ggforce
facet\u缩放

ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency)) +
    geom_bar(stat = "identity") +
    geom_text(size = 3, position = position_stack(vjust = 0.5)) +
  ggforce::facet_zoom(ylim = c(0, 50))

或具有浮动标签:

ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency)) +
    geom_bar(stat = "identity") +
    geom_text(data = within(Data, Frequency[Year == "2006-07"] <- NA), size = 3, 
              position = position_stack(vjust = 0.5)) +
  geom_label(data = Data[1:4,], aes(y = 1:4 * 100), 
            position = "stack"
ggplot(数据,不良事件(x=年份,y=频率,填充=类别,标签=频率))+
几何图形栏(stat=“identity”)+

geom_text(data=within(data,Frequency[Year==“2006-07”])您可以将
check_overlap=T
添加到
geom_text
。顺便说一句,如果您使用
geom_col()
您可以省去键入
stat=“identity”
非常好的选项!