我有一个堆积的条形图,标有geom_text。为了增加标签的可见性,我想根据标签背景的“暗度”(即条形的填充色)将标签颜色设置为白色或黑色。因此,较深的条应带有白色标签,而较浅的条应带有黑色标签。
我从问题显示代码开始,该问题在ggplot2中的堆叠条形图上显示数据值,问题的答案R中是否有浅色或深色函数?。最重要的是,我想显示数据值的标签为“白色”或“黑色”,分别取决于堆叠的条形填充颜色是深色还是深色。
我做了两次尝试。第一个是函数的使用aes(colour=...),geom_text但是这个函数失败了……为什么?
第二次尝试是使用函数scale_colour_manual。但是在这里,堆叠的条形线也使用黑色或白色切换设置“着色”。
library(ggplot2)
library(RColorBrewer)
Year <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))
Category <- c(rep(c("A", "B", "C", "D"), times = 4))
Frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)
Data <- data.frame(Year, Category, Frequency)
isDark <- function(color) {
(sum(grDevices::col2rgb(color) *c(299, 587,114))/1000 < 123)
}
## control the color assignments
paletteName <- 'Set1' # …Run Code Online (Sandbox Code Playgroud) 是否可以使用ggplot根据数据框中包含的预定标准颜色代码对颜色点进行着色?
下面是一些示例数据和代码,以帮助阐明我的问题.
tick <- c("GE","APPL","GM","BTU","WMT","JPM","LUV")
price <- c(22,900,20,22,80,31,35)
volume<- c(300,500,100,107,400,300,325)
df1 <- data.frame(ticker=tick, price=price, volume=volume)
# Here is a sample chart without colors:
p <- ggplot(df1, aes(volume, price))+
geom_point();
p
# I could use astetics and color_brewer to color points by ticker.
# But since I want to have my colors uniform across multiple plots
# outside of this script, I have specified the colors to always
# be used for certian tickers
## color speciciations
## http://wiki.stdout.org/rcookbook/Graphs/Colors%20(ggplot2)/#rcolorbrewer-palette-chart
tick<-c("GE","APPL","GM","BTU","WMT")
ccodes<-c("#3399FF", …Run Code Online (Sandbox Code Playgroud)