一个新手。
我已经使用geom_raster
绘制了如下图(data.frame
只是为了说明而创建):
require(ggplot2)
library(ggrepel)
# Create the data frame.
sales_data <- data.frame(
emp_name = rep(c("Sam", "Dave", "John", "Harry", "Clark", "Kent", "Kenneth", "Richard", "Clement", "Toby", "Jonathan"), times = 3),
month = as.factor(rep(c("Jan", "Feb", "Mar", "Jan", "Feb", "Mar", "Jan", "Feb", "Mar", "Jan", "Jan"), times = 3)),
dept_name = as.factor(rep(c("Production", "Services", "Support", "Support", "Services", "Production", "Production", "Support", "Support", "Support", "Production"), times = 3)),
revenue = rep(c(100, 200, 300, 400, 500, 600, 500, 400, 300, 200, 500), times = 3),
status = rep(c("Low", "Medium", "Medium", "High", "Very High", "Very High", "Very High", "High", "Medium", "Medium", "Low"), times = 3)
)
sales_data$month <- factor(sales_data$month, levels = c("Jan", "Feb", "Mar"))
month_vector <- levels(sales_data$month)
number_of_enteries <- nrow(sales_data)
sales_data$status <- factor(sales_data$status, levels = c("Low", "Medium", "High", "Very High"))
sales_data$month <- as.integer(sales_data$month)
ggplot(sales_data, aes(x = month, y = dept_name)) +
geom_raster(data = expand.grid(sales_data$month, sales_data$dept_name),
aes(x = Var1, y = Var2, width=1, height=1), fill = NA, col = 'gray50', lty = 1) + #default width and height is 1
#SAFE: geom_point(aes(size = revenue, col = revenue),
# shape = 16, position = position_jitter(seed = 0), show.legend = F) +
geom_point(aes(size = status, colour = cut(revenue, c(-Inf, 199, 301, Inf)) ),
shape = 16, position = position_jitter(seed = 0), show.legend = F) +
scale_color_manual(name = "revenue",
values = c("(-Inf,199]" = "red",
"(199,301]" = "#ffbf00", #amber
"(301, Inf]" = "green") ) +
geom_text(aes(label = revenue), size=4, vjust = 1.6, position = position_jitter(seed = 0)) + #try with geom_text
#geom_rect(aes(xmin = 0.5, xmax = 3.5, ymin = -1, ymax = 0.5), fill = "grey", alpha = 0.03)+
#annotate("text", x=0.5, y=-1, label= "Chart title", fontface =2) +
theme_bw() +
theme(
axis.title = element_blank(),
axis.ticks = element_blank(),
plot.background = element_blank(),
axis.line = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.text = element_text(colour = "blue", face = "plain", size =11)
) +
#coord_polar(start = 0.5, clip = 'off') +
scale_x_continuous(limits=c(0.5,3.5), expand = c(0,0), breaks = 1:length(month_vector), labels = month_vector) +
# Remove extra whitespace from y-axis so lines are against the axis
scale_y_discrete(expand = c(0,0)) +
# Add straight lines at each factor level, shifted left/down so they're between values
geom_hline(yintercept = as.numeric(sales_data$dept_name) + 0.5) +
geom_vline(xintercept = as.numeric(sales_data$month) - 0.5, color = "grey")
在给定的情节上正好出现我想要的样子,但唯一的困难是,geom_raster
不支持鼠标悬停时的ggplotly
工具提示。此外,在数据集较大的情况下,geom_points
的其他重叠很少。
这就是为什么我要使用气泡图而不是geom_raster
。但是我无法获得,该怎么办?如何将数据以网格格式分类在单个图中。 ?
此外,有什么方法可以将气泡以更有条理的方式放置在方形图块中,而不是随机(抖动)的图,有时会导致重叠。
我敢肯定,如果没有geom_raster
,有几种方法可以达到相同的结果。请帮忙!
答案 0 :(得分:1)
使用ggplot可以自定义它。不幸的是,我不知道geom_jitter()
的解决方法。
sales_data <- data.frame(
emp_name = rep(c("Sam", "Dave", "John", "Harry", "Clark", "Kent", "Kenneth", "Richard", "Clement", "Toby", "Jonathan"), times = 3),
month = as.factor(rep(c("Jan", "Feb", "Mar", "Jan", "Feb", "Mar", "Jan", "Feb", "Mar", "Jan", "Jan"), times = 3)),
dept_name = as.factor(rep(c("Production", "Services", "Support", "Support", "Services", "Production", "Production", "Support", "Support", "Support", "Production"), times = 3)),
revenue = rep(c(100, 200, 300, 400, 500, 600, 500, 400, 300, 200, 500), times = 3),
status = rep(c("Low", "Medium", "Medium", "High", "Very High", "Very High", "Very High", "High", "Medium", "Medium", "Low"), times = 3)
)
sales_data$status <- factor(sales_data$status, levels = c("Low", "Medium", "High", "Very High"),ordered = T)
sales_data$month <- factor(sales_data$month, levels = c("Jan", "Feb", "Mar"), ordered = T)
plot = sales_data%>%
ggplot(aes(x = month, y = dept_name, label = emp_name))+
geom_jitter(aes(color = revenue),width = 0.3, height = 0.3)+
geom_vline(xintercept=c(1.5,2.5))+
geom_hline(yintercept = c(1.5,2.5))+
theme_bw()+
theme(panel.grid.major = element_blank(),
axis.line = element_line(colour = "black"))+
labs(x = "Month", y = "Department Name")
plotly::ggplotly(plot)
在这里,我们首先绘制点,然后使用geom_vline
和geom_hline
添加我们自己的线。然后我们修改背景。