R - 将标签放在饼图中

时间:2022-12-03 17:56:14

Using the pie function in R, the labels appear outside of the pie. Is there a way to place labels inside each slice of the pie chart?

使用R中的饼图函数,标签显示在饼图外部。有没有办法在饼图的每个切片中放置标签?

f=table(LETTERS[c(rep(1,7),rep(2,10),rep(3,5))])
pie(f)

It shows how to do this with ggplot here place-labels-on-pie-chart. Is there no way to do it with the pie function?

它显示了如何使用ggplot这里的place-labels-on-pie-chart。用馅饼功能无法做到吗?

Edit: In response to some comments about the use of pie charts, I'd like to elaborate my intention with them. I'm actually just using the pie chart as part of a scatterplot matrix to visualize a whole data frame for regression. The matrix is set up as follows:

编辑:在回应关于使用饼图的一些评论时,我想用它们详细说明我的意图。我实际上只是使用饼图作为散点图矩阵的一部分来可视化整个数据框以进行回归。矩阵设置如下:

  • Lower panels
    Sample correlation numbers with the background colored darker for higher absolute correlation.
  • 下面板样本相关数与背景颜色较深,以获得更高的绝对相关性。
  • Upper panels
    • Scatterplots for numeric by numeric
    • Scatterplots用于数字的数字
    • Boxplots for numeric by factor
    • 数字的箱形图因子
  • 上面板Scatterplots用于数字的数字Boxplots用于数字的因子
  • Diagonal panels
    • Histograms with normal densities for numeric
    • 具有正常密度的数字直方图
    • Pie chart for factor
    • 因子饼图
  • 对角线面板具有正常密度的直方图,用于因子的数字饼图

See, I needed something for factors on the diagonal and decided on a pie chart. I decided to leave the labels out anyway and took off all axis labeling as well. I rather used darker colors for bigger pie slices... so perhaps it may be a bad way to display information but I think it works well for a factor variable, showing how the observations are proportioned across factor levels better than a barplot would do. The figure below describes the diabetes data set in the lars package.

看,我需要对角线上的因素,并在饼图上决定。我决定不管怎样留下标签,也取消了所有轴标签。我更倾向于使用较暗的颜色来制作更大的切片...所以也许它可能是显示信息的一种不好的方式,但我认为它对于因子变量很有效,显示观察结果如何在因子水平上比在条形图中更好。下图描述了lars包中的糖尿病数据集。

R  - 将标签放在饼图中

1 个解决方案

#1


4  

I don't think there is an easy way to do this as the label positions are hard coded - look at the end of

我不认为有一种简单的方法可以做到这一点,因为标签位置是硬编码的 - 看看结尾

body(pie)

        if (!is.na(lab) && nzchar(lab)) {
            lines(c(1, 1.05) * P$x, c(1, 1.05) * P$y)
            text(1.1 * P$x, 1.1 * P$y, labels[i], xpd = TRUE, 
                adj = ifelse(P$x < 0, 1, 0), ...)
        }
    }
    title(main = main, ...)
    invisible(NULL)
}

But you can overwrite this section of the program

但是你可以覆盖程序的这一部分

# create a new pie function to save overwriting original
newpie <- pie

# Tweak the label positions - changed 1.1 to 0.7
# also commented out the lines function - so that the  
# small lines next to the labels are not plot
newlbs <- quote(if (!is.na(lab) && nzchar(lab)) {
                   #lines(c(1, 1.05) * P$x, c(1, 1.05) * P$y)
                    text(0.7 * P$x, 0.7 * P$y, labels[i], xpd = TRUE, 
                                     adj = ifelse(P$x < 0, 1, 0), ...)
})

# add in the new lines of code - trial and error found the right position
body(newpie)[[22]][[4]][[7]] <- newlbs

newpie(f)

So seems a low of work but it get there and you would need to do a bit more work to angle or format the text.

所以看起来工作量很低但是它到了那里你需要做更多的工作来调整或格式化文本。

(probably turn out there is an argument to do this)

(可能会有一个论据要做到这一点)

#1


4  

I don't think there is an easy way to do this as the label positions are hard coded - look at the end of

我不认为有一种简单的方法可以做到这一点,因为标签位置是硬编码的 - 看看结尾

body(pie)

        if (!is.na(lab) && nzchar(lab)) {
            lines(c(1, 1.05) * P$x, c(1, 1.05) * P$y)
            text(1.1 * P$x, 1.1 * P$y, labels[i], xpd = TRUE, 
                adj = ifelse(P$x < 0, 1, 0), ...)
        }
    }
    title(main = main, ...)
    invisible(NULL)
}

But you can overwrite this section of the program

但是你可以覆盖程序的这一部分

# create a new pie function to save overwriting original
newpie <- pie

# Tweak the label positions - changed 1.1 to 0.7
# also commented out the lines function - so that the  
# small lines next to the labels are not plot
newlbs <- quote(if (!is.na(lab) && nzchar(lab)) {
                   #lines(c(1, 1.05) * P$x, c(1, 1.05) * P$y)
                    text(0.7 * P$x, 0.7 * P$y, labels[i], xpd = TRUE, 
                                     adj = ifelse(P$x < 0, 1, 0), ...)
})

# add in the new lines of code - trial and error found the right position
body(newpie)[[22]][[4]][[7]] <- newlbs

newpie(f)

So seems a low of work but it get there and you would need to do a bit more work to angle or format the text.

所以看起来工作量很低但是它到了那里你需要做更多的工作来调整或格式化文本。

(probably turn out there is an argument to do this)

(可能会有一个论据要做到这一点)