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”
非常好的选项!