我正在尝试在图像上绘制多个饼图。我想用custom_annotation
绘制光栅图像。但是现在我甚至无法获得多个折扣图。
最终,我希望在图像顶部的不同点上绘制6个馅饼。 imX
和imY
给出了图片在图片上的位置坐标。
head(wholebody_cutLH_wide_t[c(1,2,102,103,104)])
Acidobacteriaceae Actinomycetaceae imX imY radius
1 0.000000e+00 7.665687e-05 2.00 5.5 0.5
2 0.000000e+00 4.580237e-04 1.50 1.0 0.5
3 0.000000e+00 4.112573e-04 1.75 2.0 0.5
4 6.431473e-04 3.856008e-02 0.30 1.0 0.5
5 0.000000e+00 3.013013e-04 1.50 4.8 0.5
6 3.399756e-05 1.372986e-02 1.50 5.2 0.5
现在我的尝试是scatterpie
:
ggplot(wholebody_cutLH_wide_t) +
# annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
geom_scatterpie(aes(x=imX, y=imY,r=radius),
data=wholebody_cutLH_wide_t, cols=NA,color=sample(allcolors,101)) +
scale_color_manual(values=sample(allcolors,101)) +
scale_x_continuous(expand=c(0,0), lim=c(0,3)) +
scale_y_continuous(expand=c(0,0), lim=c(0,6)) +
theme(legend.position="none",
panel.background = element_rect(fill = "transparent") # bg of the panel
, plot.background = element_rect(fill = "transparent") # bg of the plot
, panel.grid.major = element_blank() # get rid of major grid
, panel.grid.minor = element_blank(), # get rid of minor grid
line = element_blank(),
text = element_blank(),
title = element_blank()
)
现在我的错误是:
Error: Only strings can be converted to symbols
以下是dplyr
和ggforce
的尝试:
dat_pies<-left_join(wholebody_cutLH,
wholebody_cutLH %>%
group_by(tax_rank) %>%
summarize(Cnt_total = sum(count_norm))) %>%
group_by(tax_rank) %>%
mutate(end_angle = 2*pi*cumsum(count_norm)/Cnt_total, # ending angle for each pie slice
start_angle = lag(end_angle, default = 0), # starting angle for each pie slice
mid_angle = 0.5*(start_angle + end_angle))
ggplot(dat_pies) +
geom_arc_bar(aes(x0 = imX, y0 = imY, r0 = 0, r = rpie,
start = start_angle, end = end_angle, fill = Volume)) +
geom_text(aes(x = rlabel*sin(mid_angle), y = rlabel*cos(mid_angle), label = Cnt),
hjust = 0.5, vjust = 0.5) +
coord_fixed() +
scale_x_continuous(expand=c(0,0), lim=c(0,3)) +
scale_y_continuous(expand=c(0,0), lim=c(0,6)) +
我的错误是:
Error in ggplot(dat_pies) + geom_arc_bar(aes(x0 = imX, y0 = imY, r0 = 0, :
could not find function "+<-"
任何这些方法的帮助都会很棒。 感谢
答案 0 :(得分:2)
在您第一次尝试时,您应该使用:
ggplot(wholebody_cutLH_wide_t) +
geom_scatterpie(aes(x=imX, y=imY,r=radius),
data=wholebody_cutLH_wide_t,
cols=colnames(wholebody_cutLH_wide_t)[1:2],
color=NA, alpha=.8)
答案 1 :(得分:0)
您的代码实际上并未适应您的数据。这里修复了ggforce
示例:
wholebody_cutLH_wide_t <- read.table(text = "Acidobacteriaceae Actinomycetaceae imX imY radius
0.000000e+00 7.665687e-05 2.00 5.5 0.5
0.000000e+00 4.580237e-04 1.50 1.0 0.5
0.000000e+00 4.112573e-04 1.75 2.0 0.5
6.431473e-04 3.856008e-02 0.30 1.0 0.5
0.000000e+00 3.013013e-04 1.50 4.8 0.5
3.399756e-05 1.372986e-02 1.50 5.2 0.5", header = TRUE)
library(tidyr)
library(dplyr)
library(ggforce)
# convert to long format, add a grouping variable to keep track of which pies go together
wholebody_cutLH_long <- mutate(wholebody_cutLH_wide_t, group=letters[1:6]) %>%
gather(type, amount, -imX, -imY, -radius, -group)
dat_pies<-left_join(wholebody_cutLH_long,
wholebody_cutLH_long %>%
group_by(group) %>%
summarize(amount_total = sum(amount))) %>%
group_by(group) %>%
mutate(end_angle = 2*pi*cumsum(amount)/amount_total,
start_angle = lag(end_angle, default = 0))
# now draw
ggplot(dat_pies) +
geom_arc_bar(aes(x0 = imX, y0 = imY, r0 = 0, r = radius,
start = start_angle, end = end_angle, fill = type)) +
coord_fixed()
现在在人类骨骼的图像上绘制相同的内容:
library(cowplot)
ggplot(dat_pies) +
draw_image("https://upload.wikimedia.org/wikipedia/commons/1/15/Human_skeleton_diagram.png",
x=0, y=0, width = 2.2, height = 6) +
geom_arc_bar(aes(x0 = imX, y0 = imY, r0 = 0, r = radius,
start = start_angle, end = end_angle, fill = type)) +
coord_fixed()