首页 > 解决方案 > 使用 R 在 Power BI 中渲染动态 geom_hlines 时遇到问题

问题描述

我正在尝试在 power bi 中创建一个 ggplot2 对象,该对象将根据放入和取出“值”箱的度量来呈现任意数量的水平线。我想我会用一个 for 循环来做到这一点,该循环根据数据帧的长度向对象添加一个额外的 geom_hline。我还希望每条线都有不同的颜色,以及要使用图例中的标签呈现的 hline 的值。

我使用的数据框有 3 个静态列,标题为 - Year、Escalation 和 Type。前 3 列之外的任何其他列都将被视为用于水平线的数据。

这是我目前所拥有的......

library(ggplot2)
library(RColorBrewer)

# create a unique color set
n <- 60
qual_col_pals = brewer.pal.info[brewer.pal.info$category == 'qual',]
col_vector = unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals)))

# create an dynamic x axis label depending on the number of years to be plotted

scale <- if(length(dataset$Year) < 30) {
        scale_x_continuous(breaks = seq(min(dataset$Year), max(dataset$Year)))
} else if (length(dataset$Year) >= 30  & length(dataset$Year) <= 60) {
        scale_x_continuous(breaks = seq(min(dataset$Year), max(dataset$Year), by = 2))    
} else {
        scale_x_continuous(breaks = seq(min(dataset$Year), max(dataset$Year), by = 5))
}

#ggplot object

plot <- ggplot(dataset, aes(x = Year, y = Escalation)) +

        geom_point(aes(color = "#094780"), size = 3) +

        geom_hline(aes(yintercept = mean(dataset$Escalation), color = col_vector[1]), linetype = "dashed") +

        geom_hline(aes(yintercept = median(dataset$Escalation), color = col_vector[2]), linetype = "dashed") +

        theme(axis.text.x = element_text(colour = "#942832")) +
        theme(axis.text.y = element_text(colour = "#942832")) + 

        scale +

        scale_y_continuous(breaks = round(seq(min(dataset$Escalation), max(dataset$Escalation), by = 0.02),2))

# add horizontal comparison lines

addline <- function(data){
        c <- list(unlist(unique(dataset[3])), paste("Mean ", round(mean(dataset$Escalation), 3)), paste("Median", round(median(dataset$Escalation),3)))
        t <- list("#094780", col_vector[1], col_vector[2])
        for (i in 1:data){
                line = geom_hline(aes(yintercept = dataset[1,3+i], color = col_vector[2+i]))

                plot = plot + line

                c[3+i] = paste(unlist(names(dataset)[3+i]), " Escalation :", round(dataset[1 ,3+i], 3))
                t[3+i] = col_vector[2+i]
        }

    m = scale_color_manual(name = "", values = t, labels = c)

    plot = plot + m

    return(plot)
}

addline(NCOL(dataset)-3)

它正在渲染,但当我为超过 1 条水平线添加数据时,它并没有给我我所期望的(它用数据移动线,但没有正确命名或着色)。作为参考,如果有 2 条水平线并且是硬编码的,我希望代码看起来像这样(这在 Power BI 中正确呈现)。

library(ggplot2)
library(RColorBrewer)

n <- 60
qual_col_pals = brewer.pal.info[brewer.pal.info$category == 'qual',]
col_vector = unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals)))

scale <- if(length(dataset$Year) < 30) {
        scale_x_continuous(breaks = seq(min(dataset$Year), max(dataset$Year)))
} else if (length(dataset$Year) >= 30  & length(dataset$Year) <= 60) {
        scale_x_continuous(breaks = seq(min(dataset$Year), max(dataset$Year), by = 2))    
} else {
        scale_x_continuous(breaks = seq(min(dataset$Year), max(dataset$Year), by = 5))
}

plot <- ggplot(dataset, aes(x = Year, y = Escalation)) +

        geom_point(aes(color = "#094780"), size = 3) +

        geom_hline(aes(yintercept = mean(dataset$Escalation), color = col_vector[1]), linetype = "dashed") +

        geom_hline(aes(yintercept = median(dataset$Escalation), color = col_vector[2]), linetype = "dashed") +

        theme(axis.text.x = element_text(colour = "#942832")) +
        theme(axis.text.y = element_text(colour = "#942832")) + 

        scale +

        scale_y_continuous(breaks = round(seq(min(dataset$Escalation), max(dataset$Escalation), by = 0.02),2)) +

        geom_hline(aes(yintercept = dataset[1,4], color = col_vector[3]),) +

        geom_hline(aes(yintercept = dataset[1,5], color = col_vector[4]),) +

        scale_color_manual(
                name = "", 
                values = list("#094780", col_vector[1], col_vector[2], col_vector[3], col_vector[4]), 
                labels = list(unlist(unique(dataset[3])), 
                        paste("Mean ", round(mean(dataset$Escalation), 3)), 
                        paste("Median", round(median(dataset$Escalation),3)), 
                        paste(unlist(names(dataset)[4]), " Escalation :", round(dataset[1 ,4], 3)), 
                        paste(unlist(names(dataset)[5]), " Escalation :", round(dataset[1 ,5], 3))
                )
        )

plot

在编码方面我仍然是新手,所以我很确定我只是不了解循环如何工作的基本知识。

我知道这有点转变,但我很难真正调试,因为我必须将数据集从 power BI 导出到 r studio。任何帮助表示赞赏!

标签: rggplot2powerbi

解决方案


推荐阅读