问题描述
我有数据,我需要复制这样的图形。我不确定这种绘图的类型是什么,因此我也不知道如何在R中进行绘图。
解决方法
我不知道它叫什么,但是这里有一个产生一个的函数:
library(ggplot2)
quadrant_plot <- function(component,values,fills = c("#919191","#686868","#434343","#ededed"))
{
df <- data.frame(component = factor(component,levels = component),xvalue = c(values[1:2],-values[3:4]),yvalue = c(values[1],-values[2:3],values[4]),xmin = c(0,-Inf,-Inf),xmax = c(Inf,Inf,0),ymin = c(0,ymax = c(Inf,Inf),textx = c(50,50,-50,-50),texty = c(50,50),labelx = c(95,95,-95,-95),labely = c(95,95),hjust = c(1,1,0))
ggplot(df,aes(xvalue,yvalue)) +
geom_rect(aes(xmin = xmin,xmax = xmax,ymin = ymin,ymax = ymax,fill = component)) +
geom_point() +
geom_polygon(fill = NA,color = "black",size = 1) +
geom_text(aes(x = textx,y = texty,label = abs(xvalue))) +
geom_text(aes(x = labelx,y = labely,label = component,hjust = hjust),size = 6) +
geom_hline(aes(yintercept = 0),size = 1) +
geom_vline(aes(xintercept = 0),size = 1) +
scale_fill_manual(values = fills,guide = guide_none()) +
lims(x = c(-100,100),y = c(-100,100)) +
coord_equal() +
theme_void()
}
因此您可以这样做:
quadrant_plot(component = c("Adhocracy","Hierarchy","Market","Clan"),values = c(18.4,22.9,32.6,26.5))
或者如果您想更改输入和颜色,可以
quadrant_plot(component = c("Optimism","Insight","Skill","Luck"),values = c(35,15,12,31),fills = c("gold","deepskyblue4","orange","lightblue"))