【问题标题】:Plotrix labels, adjusting the size/font/position?Plotrix 标签,调整大小/字体/位置?
【发布时间】:2020-03-25 10:35:10
【问题描述】:

我一直在尝试使用 plotly 和现在的 plotrix 创建风险雷达,我遇到了两者的限制(基于我的需要以及我对 R 的技能)。

有了 plotly,我几乎拥有了我想要的大部分东西,除了交易破坏者无法标记径向轴(团队 A、团队 B 等)

我使用 plotrix 的版本几乎是我需要的地方,只需要一些指导来帮助我完成任务吗?

我有 4 个问题:

  1. 标题可以左右移动吗?

  2. 径向?标签渗入图表圈使其变得困难 阅读,可以以某种方式调整它们吗?

  3. 是否可以更改标签的字体(大小和/或颜色)0/30/60/90/180?

  4. 是否有向绘图点添加文本,在我的情况下,我 想要将 RiskID 作为标签

我的图表如下所示:

library(plotrix)

# Build sample dataset
aRiskID      <- c(1, 15, 23, 28, 35)
bRiskDays    <- as.numeric(c(28, 15, 85, 153, 100))
cTheta       <- as.integer(c(20, 80, 130, 240, 320))
dConsequence <- c("Major", "Major", "Minor", "Moderate", "Minor")

myRisks <- data.frame(RiskID = aRiskID, RiskDays = bRiskDays, Theta = cTheta, CurrentConsequence = dConsequence)

myLabels <- c("Team A", "Team B", "Team C", "Team D", "Team E", "Team F", "Team G", "Team H")

# Test different point colours
# initializing vector of colors as NA
colors_plot <- rep(NA,length(myRisks))

# set of conditions listed in the plot
colors_plot[myRisks$CurrentConsequence == "Major"]    <- "black"
colors_plot[myRisks$CurrentConsequence == "Moderate"] <- "red"
colors_plot[myRisks$CurrentConsequence == "Minor"]    <- "green"
# add more conditions as needed

# par(mar=c(2,5,5,5))

# plot the chart
radial.plot(myRisks$RiskDays,
            myRisks$Theta,
            start = pi/2,
            clockwise = FALSE,
           # start=pi/2,clockwise=TRUE,
            show.grid.labels=1,
            rp.type="s",
            main="Risk Radar",
            radial.lim=c(0,30,60,90,180),
            radlab = TRUE,
            point.symbols=17,
            point.col=colors_plot,
            cex = 2,
            cex.axis = 0.25,
            cex.lab = 0.25,
            lwd=par("lwd"),mar=c(2,2,3,2),
           # show.centroid=TRUE,
            labels=myLabels)

我不知道还有什么地方可以使用这个,所以任何使用 plotrix 或其他图表包来实现最终结果的技巧都会很棒。

【问题讨论】:

    标签: r plotrix


    【解决方案1】:

    你应该看看函数radial.plot.labelsradial.grid

    # plot the chart
    radial.plot(myRisks$RiskDays,
                myRisks$Theta,
                start = pi/2,
                clockwise = FALSE,
                # start=pi/2,clockwise=TRUE,
                show.grid.labels=1,
                rp.type="s",
                # main="Risk Radar",
                radial.lim=c(0,30,60,90,180),
                radial.labels = '',
                radlab = TRUE,
                point.symbols=17,
                point.col=colors_plot,
                cex = 2,
                cex.axis = 0.25,
                cex.lab = 0.25,
                lwd=par("lwd"),mar=c(2,2,3,2),
                # show.centroid=TRUE,
                labels=NULL, label.pos = pi / 4 * 2:9)
    # 1
    mtext("Risk Radar", at = par('usr')[1], font = 2)
    # 2
    at <- c(0,30,60,90,180)
    radial.plot.labels(max(at) + 35, pi / 4 * 2:9, labels = myLabels, radial.lim = at)
    # 3
    radial.plot.labels(at, pi / 2 * 3, labels = at, col = 1:5, cex = 1.5)
    # 4
    radial.plot.labels(myRisks$RiskDays, myRisks$Theta, start = pi/2,
                       clockwise = FALSE, labels = myRisks$RiskID)
    

    如果您真的需要垂直标签,您可以使用 radial.grid 函数或通过单独的旋转 (srt) 循环遍历标签。 srt 没有在 text 中矢量化,这真是太遗憾了,这会让这变得容易得多

    th <- pi / 4 * 2:9
    sapply(seq_along(th), function(ii) {
      i <- ifelse((th[ii] > pi / 2) & (th[ii] < pi / 2 * 3), pi, 0)
      radial.plot.labels(max(at) + 35, th[ii], labels = myLabels[ii],
                         radial.lim = at, srt = (th[ii] - i) * 180 / pi)
    })
    

    我不小心做了这个可爱的雪花@accidental__aRt

    th <- pi / 4 * 2:9
    sapply(th, function(x)
      radial.plot.labels(max(at) + 35, pi / 4 * 2:9, labels = myLabels,
                         radial.lim = at, srt = x * 180 / pi))
    

    【讨论】:

    • 天哪,这简直太耸人听闻了!我一直在尝试很多方法来实现我的最终目标,我已经浪费了 20 多个小时蹒跚而行,我花了一半以上的时间来尝试,并且非常接近。我只需要弄清楚如何减小 30/60/90 的字体大小,我就会有我的图表!!!非常感谢您向我指出这一点
    • 其实...只是注意到最后一条语句中的 cex 参数;像魅力一样工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 2018-10-16
    相关资源
    最近更新 更多