在 Power BI 中使用 R 的热图 - 缺失值

时间:2021-06-09 08:05:04

标签: r ggplot2 powerbi heatmap

我正在寻求有关 Power BI 中 R 视觉效果的帮助。我正在创建一个热图,查看年龄和工资范围(10k 分组)之间的关系。我有与缺失数据相关的空白行/列。我正在使用完整的函数来填充缺失值,但这仅适用于数据中已经存在年龄/工资范围组合的情况。在它不存在的地方,我得到一个空白的行或列。

任何帮助将不胜感激。

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: 

# dataset <- data.frame(Age, Salary, ID )
# dataset <- unique(dataset)

# Paste or type your script code here:

                          
library(ggplot2)    
library(ggthemes)   # has a clean theme for ggplot2
library(tidyr)      # provide 'complete' function for empty heatmap cells
library(viridis)    # for colour scale
library(scales)     # to provide breaks and formatting options for axis


#Put in zeros for missing data values, to avoid blank cells in the heatmap
dataset <- dataset %>% 
complete(Age, Salary, fill = list(ID = 0))

attach(dataset)

ggplot(dataset, aes(x=Age, y=Salary, fill=ID)) +
geom_tile(size=0.1) + 
scale_fill_viridis(name="", option="plasma") +
scale_x_continuous(breaks=seq(0,75,5), limits=c(0,75)) +
scale_y_continuous(breaks=seq(0,400000,20000),labels=scales::dollar_format(prefix="£")) +

labs(x=NULL, 
y=NULL) +

theme(
plot.title = element_blank(),
plot.subtitle = element_blank(),
    panel.background = element_blank(),
    axis.text = element_text(family="Arial",face="plain",size=14),
    axis.ticks = element_blank(),
    legend.position = "bottom",
    legend.text = element_text(color = "#333333", size = 12),
    legend.key.size = unit(0.75, "cm"),
  legend.key.width = unit(1.0,"cm") )

0 个答案:

没有答案