2

我正在尝试在图像顶部绘制多个饼图。我想用来custom_annotation绘制光栅图像。但现在我什至无法获得多个饼图。

最终,我希望在图像顶部的不同点绘制 6 个饼图。imXimY给出饼图应该在图像上的坐标。

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这是我对and的尝试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 "+<-"

对这两种方法的任何帮助都会很棒。谢谢

4

2 回答 2

3

在您的第一次尝试中,您应该使用:

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)

在此处输入图像描述

于 2017-12-08T17:15:24.250 回答
1

看起来您实际上并未根据数据调整代码。这是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()

在此处输入图像描述

于 2017-12-08T22:43:22.137 回答