我的数据集
我的要求
基本上,我需要一个简单的三角形图表,其中高度是No column和Group Name in框中的值。宽度可以是任何尺寸。如果我们可以为不同的套装提供不同的颜色,那就太棒了。
我是R的新手,并尝试过polygon
,lapply
,geom_polygon
,triangle
,pascal triangle
,ade4
,{{ 1}}和其他人,但没有成功。
此外,在R Studio中,这应该是一个视觉效果,而不是观看者视觉效果。
答案 0 :(得分:5)
我找不到一个好的包装,但我能够制作情节。对不起,我没有使用R Studio,所以我不确定你的最后一点意味着什么。
plot.pyramid <- function(df, hspace=2, colours=NA){
n <- nrow(df) #number of observations
#i don't know what you want to do with colors
if(any(is.na(colours))) colours <- rep('blue', n)
if(length(colours)<n) colours <- rep(colours,n)[1:n]
cum.height <- cumsum(df$no) #cumulative height. y coordinates in plot
max.cum.height <- max(cum.height) #max height of the graph
center <- max(cum.height)/2 #where to center the x values
#there are 2 y values per point to make the horizontal segments
y <- max.cum.height - rep(cum.height,2)
x <- c(center-cum.height/2, center + cum.height/2) #horizontal segments are same size as height, centered
x <- c(x, center) #add x-coord for tip
y <- c(y, max.cum.height) #add y-coord for tip
reorder <- order(y) #reorder points for convenience in 'for loop' below
y <- y[reorder]
x <- x[reorder]
plot(x,y,'n', xaxt='n', yaxt='n', ann = FALSE) #creates blank plot of correct size
for(i in 0:(n-1) ){
if(i != n-1) #if not tip
{
xcoords <- x[i*2+1:4][c(1,3,4,2)] #reorder the points slightly to get a trapezoid fill
ycoords <- y[i*2+1:4][c(1,3,4,2)]
#add the space between the trapezoids
ycoords[ycoords==min(ycoords)] <- ycoords[ycoords==min(ycoords)] + hspace
}else{
xcoords <- x[i*2+1:3] #triangle only has 3 points
ycoords <- y[i*2+1:3]
ycoords <- ycoords + hspace
}
polygon(xcoords,ycoords,col=colours[i+1]) #plot points and fill
text(mean(xcoords), mean(ycoords), cex=.8, paste(df$group[n-i], df$no[n-i]) ) #add text
}
}
df <- data.frame(group=c(paste0('Group-',seq(5))), no=c(5,25,36,40,50))
colours <- c('blue','green','blue','gray','green')
plot.pyramid(df, 2, colours)
答案 1 :(得分:1)
使用ggplot2
library(data.table)
library(ggplot2)
dt.triangle <- data.table(group = c(1,1,1), polygon.x = c(2,4,3), polygon.y = c(1,1,3))
ggplot()+
geom_polygon(data = dt.triangle,
aes( x=polygon.x,y=polygon.y,group=group), fill="#3182bd") +
geom_segment(aes(x = 2.24, y = 1.5, xend = 3.78, yend = 1.5),
size=2, color="white") +
geom_segment(aes(x = 2.49, y = 2, xend = 3.52, yend = 2),
size=2, color="white") +
geom_segment(aes(x = 2.74, y = 2.5, xend = 3.26, yend = 2.5),
size=2, color="white") +
geom_segment(aes(x = 2.865, y = 2.75, xend = 3.134, yend = 2.75),
size=2, color="white") +
annotate("text", x = 3, y = 1.25, label = "Group-5 50", size=3) +
annotate("text", x = 3, y = 1.75, label = "Group-4 40", size=3) +
annotate("text", x = 3, y = 2.25, label = "Group-3 36", size=3) +
annotate("text", x = 3, y = 2.65, label = "Group-2 25", size=3) +
annotate("text", x = 3, y = 2.85, label = "Group-1 5", size=3) +
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
plot.background=element_blank())