我想用圆圈作为轴来叠加我的情节,以说明概率等级(例如0.25; 0.75,1)。
要重现图形,您需要在工作目录中使用这两个csv文件
https://drive.google.com/open?id=1RsleBYQFlm3ce3xuqTLK-_r9s374yd40
或者@Gregor在评论中给我的好意见,这里是我的数据对象的标题,所以不需要下载:
dput(头(数据))
structure(list(id = 1:6, individual = structure(c(1L, 12L, 23L,
26L, 27L, 28L), .Label = c("Person 1", "Person 10", "Person 11",
"Person 12", "Person 13", "Person 14", "Person 15", "Person 16",
"Person 17", "Person 18", "Person 19", "Person 2", "Person 20",
"Person 21", "Person 22", "Person 23", "Person 24", "Person 25",
"Person 26", "Person 27", "Person 28", "Person 29", "Person 3",
"Person 30", "Person 31", "Person 4", "Person 5", "Person 6",
"Person 7", "Person 8", "Person 9"), class = "factor"), value = c(0.658333333,
0.958333333, 0.720833334, 0.883333333, 0.779166667, 0.9375),
group = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "A", class = "factor")), .Names = c("id",
"individual", "value", "group"), row.names = c(NA, 6L), class = "data.frame")
和第二个对象:
dput(头(label_data))
structure(list(id = 1:6, individual = structure(c(1L, 12L, 23L,
26L, 27L, 28L), .Label = c("Person 1", "Person 10", "Person 11",
"Person 12", "Person 13", "Person 14", "Person 15", "Person 16",
"Person 17", "Person 18", "Person 19", "Person 2", "Person 20",
"Person 21", "Person 22", "Person 23", "Person 24", "Person 25",
"Person 26", "Person 27", "Person 28", "Person 29", "Person 3",
"Person 30", "Person 31", "Person 4", "Person 5", "Person 6",
"Person 7", "Person 8", "Person 9"), class = "factor"), value = c(0.658333333,
0.958333333, 0.720833334, 0.883333333, 0.779166667, 0.9375),
group = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "A", class = "factor"),
hjust = c(0, 0, 0, 0, 0, 0), angle = c(84.375, 73.125, 61.875,
50.625, 39.375, 28.125)), .Names = c("id", "individual",
"value", "group", "hjust", "angle"), row.names = c(NA, 6L), class = "data.frame")
然后运行以下:
library(tidyverse)
library(ggplot2)
library(plotrix)
data=read.csv(file="data_object_2.csv", header=TRUE, sep=",")
label_data=read.csv(file="label_data_object_2.csv", header=TRUE, sep=",")
empty_bar=1
to_add = data.frame( matrix(NA, empty_bar*nlevels(data$group), ncol(data)) )
colnames(to_add) = colnames(data)
to_add$group=rep(levels(data$group), each=empty_bar)
data=rbind(data, to_add)
data=data %>% arrange(group)
data$id=seq(1, nrow(data))
number_of_bar=nrow(label_data)
angle= 90 - 360 * (label_data$id-0.5) /32
label_data$hjust<-ifelse( angle < -90, 1, 0)
label_data$angle<-ifelse(angle < -90, angle+180, angle)
p = ggplot(data, aes(x=as.factor(id), y=value)) +
geom_bar(stat="identity", fill=alpha("skyblue", 0.7)) +
ylim(-0.3,1) +
theme_minimal() +
theme(
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
plot.margin = unit(rep(-1,4), "cm")
) +
coord_polar(start = 0) +
geom_text(data=label_data, aes(x=id, y=value, label=individual, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE ) +
geom_vline(xintercept = 0, color = "grey", linetype = "dashed") +
annotate("text", label = "p=0", x = 0, y = 0, color = "black") +
annotate("text", label = "p=1", x = 0, y = 1, color = "black") +
annotate("text", label = "p=0.5", x = 0, y = 0.5, color = "black")
p
这将导致: https://drive.google.com/open?id=1xDOym_nn-x9nrUoKpB9rtg7h7NYIfucF
我想用圆圈覆盖,表示提高可读性的概率级别。我发现的所有在线帮助都与常见的笛卡尔图或geom_circle函数有关,这些函数也不起作用。
我真的很感激任何帮助。
感谢Marek
答案 0 :(得分:0)
这是一个关于代码简化版本的选项(我不想重新创建所有标签等,只是图表的简化版本)。我这样想:如果这是在常规的笛卡尔坐标系中,你可以通过制作一条水平线来显示概率的叠加,所以在极坐标系中,该直线会变成圆形。添加geom_hline
会为您设置的yintercept
提供一个圆圈。
标记这些概率可能会很好;你可以弄清楚在你的上下文中最好的方法是什么,但我只做了几个圆圈,将y-break设置为相同的值,并将y轴标题移动到标签附近,这样他们就有了一点点说明。根据您的背景情况,这可能并非都是必要的。
另外,我建议将这两个数据框合并为一个,这样您就可以更轻松地跟踪事物,而不必在不同的geoms中设置不同的data =
参数。
library(tidyverse)
label_data %>%
ggplot(aes(x = individual, y = value)) +
geom_col(width = 0.5, fill = "skyblue", alpha = 0.7) +
geom_hline(yintercept = c(0.5, 0.75, 0.9), color = "gray60") +
scale_y_continuous(limits = c(-0.3, NA), breaks = c(0.5, 0.75, 0.9)) +
theme_minimal() +
theme(panel.grid = element_blank(), axis.title.y = element_text(hjust = 0.87)) +
coord_polar(start = 0) +
labs(x = NULL, y = "Probability")
由reprex package(v0.2.0)创建于2018-06-03。