使用可选箭头创建最佳饼图布局
Creating best pie chart layout using optional arrows
我只是想知道为饼图创建适当布局的一种想法。看看下面的代码:
library(ggplot2)
pie_chart=function(vec){
if (class(vec)=='numeric'){vec<-as.character(vec)
vec<-print(paste('var',vec))
}
#creating data frame : variables and number of their occurrences
df<-data.frame(table(vec))
colnames(df)[1]<-'group'
#creating bar plot first
bp<- ggplot(df, aes(x="", y=Freq, fill=group))+
geom_bar(width = 1, stat = "identity")
blank_theme <- theme_minimal()+
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank(),
plot.title=element_text(size=14, face="bold")
)
#adding polar coordinates to make a circle
pie <- bp + coord_polar("y", start=0)
pie + scale_fill_brewer("Characteristic") + blank_theme +
theme(axis.text.x=element_blank())+
geom_text(aes(y = rev(Freq)/2 + c(0, cumsum(rev(Freq))[-length(Freq)]),
label = print(paste0(rev(Freq),'(',percent(rev(Freq)/(sum(Freq))),')'))), size=5)
}
pie_chart(c(rep(1,40),rep(2,30),rep(3,90),rep(8,2)))
如您所见,上面的数字“2(1.2%)”被其他数字所支配,这对我们的饼图有明显的不良影响。我想改变我的功能,如果某个值小于所有饼图的 5%,则该值应该在饼图之外,箭头指向饼图的适当部分,其他值不变(在饼图内)。
所以在我们的例子中,'2(1.2%)' 不应该在馅饼中,而是在馅饼之外,箭头指向馅饼的上侧。你知道怎么做吗?
提前致谢!
这是一种解决方案,通过使用 scales::oob_keep()
并关闭坐标裁剪,将文本标签置于 x-axis 限制之外。您可以使用 arrow()
函数控制箭头的外观。
library(ggplot2)
library(scales)
pie_chart=function(vec){
if (class(vec)=='numeric'){vec<-as.character(vec)
vec<-print(paste('var',vec))
}
#creating data frame : variables and number of their occurrences
df<-data.frame(table(vec))
colnames(df)[1]<-'group'
#creating bar plot first
bp<- ggplot(df, aes(x=1, y=Freq, fill=group))+
geom_bar(width = 1, stat = "identity")
blank_theme <- theme_minimal()+
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
plot.title=element_text(size=14, face="bold")
)
#adding polar coordinates to make a circle
pie <- bp + coord_polar("y", start=0, clip = "off")
pie + scale_fill_brewer("Characteristic") + blank_theme +
theme(axis.text.x=element_blank())+
geom_text(aes(y = rev(Freq)/2 + c(0, cumsum(rev(Freq))[-length(Freq)]),
label = print(paste0(rev(Freq),'(',percent(rev(Freq)/(sum(Freq))),')')),
x = ifelse(rev(Freq) / sum(Freq) < 0.05, 1.7, 1)),
size=5) +
geom_segment(aes(x = 1.6, xend = 1.5,
y = rev(Freq)/2 + c(0, cumsum(rev(Freq))[-length(Freq)]),
yend = after_stat(y),
colour = I(ifelse(rev(Freq) / sum(Freq) < 0.05, "black", "transparent"))),
arrow = arrow(length = unit(1, "mm"))) +
scale_x_continuous(limits = c(0.5, 1.5), oob = scales::oob_keep)
}
pie_chart(c(rep(1,40),rep(2,30),rep(3,90),rep(8,2)))
由 reprex package (v0.3.0)
于 2020-08-11 创建
我只是想知道为饼图创建适当布局的一种想法。看看下面的代码:
library(ggplot2)
pie_chart=function(vec){
if (class(vec)=='numeric'){vec<-as.character(vec)
vec<-print(paste('var',vec))
}
#creating data frame : variables and number of their occurrences
df<-data.frame(table(vec))
colnames(df)[1]<-'group'
#creating bar plot first
bp<- ggplot(df, aes(x="", y=Freq, fill=group))+
geom_bar(width = 1, stat = "identity")
blank_theme <- theme_minimal()+
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank(),
plot.title=element_text(size=14, face="bold")
)
#adding polar coordinates to make a circle
pie <- bp + coord_polar("y", start=0)
pie + scale_fill_brewer("Characteristic") + blank_theme +
theme(axis.text.x=element_blank())+
geom_text(aes(y = rev(Freq)/2 + c(0, cumsum(rev(Freq))[-length(Freq)]),
label = print(paste0(rev(Freq),'(',percent(rev(Freq)/(sum(Freq))),')'))), size=5)
}
pie_chart(c(rep(1,40),rep(2,30),rep(3,90),rep(8,2)))
如您所见,上面的数字“2(1.2%)”被其他数字所支配,这对我们的饼图有明显的不良影响。我想改变我的功能,如果某个值小于所有饼图的 5%,则该值应该在饼图之外,箭头指向饼图的适当部分,其他值不变(在饼图内)。
所以在我们的例子中,'2(1.2%)' 不应该在馅饼中,而是在馅饼之外,箭头指向馅饼的上侧。你知道怎么做吗?
提前致谢!
这是一种解决方案,通过使用 scales::oob_keep()
并关闭坐标裁剪,将文本标签置于 x-axis 限制之外。您可以使用 arrow()
函数控制箭头的外观。
library(ggplot2)
library(scales)
pie_chart=function(vec){
if (class(vec)=='numeric'){vec<-as.character(vec)
vec<-print(paste('var',vec))
}
#creating data frame : variables and number of their occurrences
df<-data.frame(table(vec))
colnames(df)[1]<-'group'
#creating bar plot first
bp<- ggplot(df, aes(x=1, y=Freq, fill=group))+
geom_bar(width = 1, stat = "identity")
blank_theme <- theme_minimal()+
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
plot.title=element_text(size=14, face="bold")
)
#adding polar coordinates to make a circle
pie <- bp + coord_polar("y", start=0, clip = "off")
pie + scale_fill_brewer("Characteristic") + blank_theme +
theme(axis.text.x=element_blank())+
geom_text(aes(y = rev(Freq)/2 + c(0, cumsum(rev(Freq))[-length(Freq)]),
label = print(paste0(rev(Freq),'(',percent(rev(Freq)/(sum(Freq))),')')),
x = ifelse(rev(Freq) / sum(Freq) < 0.05, 1.7, 1)),
size=5) +
geom_segment(aes(x = 1.6, xend = 1.5,
y = rev(Freq)/2 + c(0, cumsum(rev(Freq))[-length(Freq)]),
yend = after_stat(y),
colour = I(ifelse(rev(Freq) / sum(Freq) < 0.05, "black", "transparent"))),
arrow = arrow(length = unit(1, "mm"))) +
scale_x_continuous(limits = c(0.5, 1.5), oob = scales::oob_keep)
}
pie_chart(c(rep(1,40),rep(2,30),rep(3,90),rep(8,2)))
由 reprex package (v0.3.0)
于 2020-08-11 创建