着色方案饼图,ggplot2
Coloring scheme pie chart, ggplot2
我正在用 ggplot2
制作饼图,但我无法正确设置饼图切片的颜色。
df <- data.frame(group = c("Information technology", "Laboratory", "Tools",
"Unspecified", "Other"),
value = c(2500, 2000, 1500, 1000, 4000))
# Coloring scheme
blue1 <- "#0a3250" # darkest blue
blue2 <- "#13517b"
blue3 <- "#2f659f"
blue4 <- "#518ecb"
blue5 <- "#aac8df"
blue6 <- "#d6e7f2" # lightest blue
colorscheme <- scale_fill_manual(values = c(blue1, blue2, blue3,
blue4, blue5, blue6))
# Data labels:
df <- df[!is.na(df$value),] # Only keeps non-zero segments
lbls <- df$value/sum(df$value)
lbls <- percent(lbls) # Converts to string and adds % sign
lbls[lbls == "0.0%"] <- "" # Removes 0.0% as a label
# we now actually plot the data:
ggplot(df, aes(x = "", y = value, fill = group)) +
colorscheme +
geom_bar(width = 1, stat = "identity", col = "white") +
xlab("") + ylab("") +
theme_bw() +
geom_text(aes(x = 1.90,
y = value/2 + c(0, cumsum(value)[-length(value)]),
label = lbls), size = 4) +
coord_polar(theta = "y") +
theme(
axis.text = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
legend.key = element_rect(fill=NULL, colour = "white"),
legend.key.size = unit(0.5, "cm"),
legend.title = element_blank(),
legend.text = element_text(size = 9)
)
我想要的:
我希望 df
中的第一个条目的颜色为 blue1
,第二个条目的颜色为 blue2
,以此类推。这样切片按顺时针顺序从最深的蓝色到最浅的蓝色.
此外,图例应按 df
.
中的相同顺序显示组
问题是什么:
但是,颜色不是根据 df
中的位置分配的,而是根据字母顺序分配的。现在,不同深浅的蓝色散布在饼图和图例上。
为您的分类变量使用一个因子:
df <- data.frame(group=factor(x=1:5,
labels=c("Information technology", "Laboratory", "Tools", "Unspecified", "Other")),
value = c(2500, 2000, 1500, 1000, 4000))
我正在用 ggplot2
制作饼图,但我无法正确设置饼图切片的颜色。
df <- data.frame(group = c("Information technology", "Laboratory", "Tools",
"Unspecified", "Other"),
value = c(2500, 2000, 1500, 1000, 4000))
# Coloring scheme
blue1 <- "#0a3250" # darkest blue
blue2 <- "#13517b"
blue3 <- "#2f659f"
blue4 <- "#518ecb"
blue5 <- "#aac8df"
blue6 <- "#d6e7f2" # lightest blue
colorscheme <- scale_fill_manual(values = c(blue1, blue2, blue3,
blue4, blue5, blue6))
# Data labels:
df <- df[!is.na(df$value),] # Only keeps non-zero segments
lbls <- df$value/sum(df$value)
lbls <- percent(lbls) # Converts to string and adds % sign
lbls[lbls == "0.0%"] <- "" # Removes 0.0% as a label
# we now actually plot the data:
ggplot(df, aes(x = "", y = value, fill = group)) +
colorscheme +
geom_bar(width = 1, stat = "identity", col = "white") +
xlab("") + ylab("") +
theme_bw() +
geom_text(aes(x = 1.90,
y = value/2 + c(0, cumsum(value)[-length(value)]),
label = lbls), size = 4) +
coord_polar(theta = "y") +
theme(
axis.text = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
legend.key = element_rect(fill=NULL, colour = "white"),
legend.key.size = unit(0.5, "cm"),
legend.title = element_blank(),
legend.text = element_text(size = 9)
)
我想要的:
我希望 df
中的第一个条目的颜色为 blue1
,第二个条目的颜色为 blue2
,以此类推。这样切片按顺时针顺序从最深的蓝色到最浅的蓝色.
此外,图例应按 df
.
问题是什么:
但是,颜色不是根据 df
中的位置分配的,而是根据字母顺序分配的。现在,不同深浅的蓝色散布在饼图和图例上。
为您的分类变量使用一个因子:
df <- data.frame(group=factor(x=1:5,
labels=c("Information technology", "Laboratory", "Tools", "Unspecified", "Other")),
value = c(2500, 2000, 1500, 1000, 4000))