英文:
How to set the color value in a plot based on 2 conditions ggplot
问题
以下是代码的翻译部分:
我试图制作一个分组的柱状图。我重复使用了一个之前有效的脚本,但在那个脚本中,我不需要根据条件着色。这是我现在尝试并且失败的内容。
我希望根据条件和值是低于还是高于0来着色柱状图。
示例数据:
library(ggplot2)
library(reshape2)
library(Rcpp)
prot_names <- c("Prot_A", "Prot_B", "Prot_C")
Expression_Con_A <- c(-1, 2, 1)
Expression_Con_B <- c(-2, 1.5, 0.5)
df <- data.frame(prot_names, Expression_Con_A, Expression_Con_B)
melt_dat <- melt(df)
我想象中的代码是:
for (i in 1:length(melt_dat$value)) {
if (melt_dat$value[i] > 0 && melt_dat$variable[i] == "Con_A") {
melt_dat$color[i] <- "skyblue1"
} else if (melt_dat$value[i] < 0 && melt_dat$variable[i] == "Con_A") {
melt_dat$color[i] <- "red1"
} else if (melt_dat$value[i] > 0 && melt_dat$variable[i] == "Con_B") {
melt_dat$color[i] <- "skyblue4"
} else if (melt_dat$value[i] < 0 && melt_dat$variable[i] == "Con_B") {
melt_dat$color[i] <- "red4"
}
}
然而,这给了我以下错误:
错误:条件的长度大于1
https://stackoverflow.com/questions/43599859/ggplot2-how-to-color-a-graph-by-multiple-variables
这种方法在某种程度上有效,但它给了我柱子和框架不同的颜色,这不是我想要的。
我还尝试手动设置颜色:
g <- ggplot(melt_dat, aes(x = value, y = prot_names, fill = variable)) +
geom_bar(stat = 'identity', position = 'dodge') +
scale_fill_manual(values = c("red1", "skyblue1", "red1", "red4", "skyblue4", "red4"))
但这也不起作用,因为它只根据条件着色(独立于值>0)。
请注意,我已经修正了你的循环和变量名称,以确保代码正确运行。但你可能需要进一步调整以满足你的需求。
英文:
I am trying to make a grouped barchart. I've reused an older script that worked before, however in that script i didn't have to color based on conditions. This is what I am trying now and failing at.
I want the bars to be coloured based on the Condition and if the value is below or above 0
Example data:
library( ggplot2)
library(reshape2)
library(Rcpp)
prot_names <- c("Prot_A", "Prot_B","Prot_C")
Expression_Con_A <- c(-1,2,1)
Expression_Con_B <- c(-2,1.5,0.5)
df <- data.frame(prot_names, Expression_Con_A, Expression_Con_B)
melt_dat <- melt((df))
I'm imagining something like:
for (i in melt_dat$value) {
if (melt_dat$value[i] > 0 & melt_dat$variable== "ConA")
{melt_dat$color<- skyblue1}
else if (melt_dat$value[i] < 0 & melt_dat$variable== "ConA")
{melt_dat$color<- red1}
else if (melt_dat$value[i] > 0 & melt_dat$variable== "ConB")
{melt_dat$color<- skyblue4}
else if (melt_dat$value[i] < 0 & melt_dat$variable== "ConB")
{melt_dat$color<- red4}
}
However this gives me the folowing error:
Error in if (melt_dat$value[i] > 0 & melt_dat$variable == "ConA") { :
the condition has length > 1
https://stackoverflow.com/questions/43599859/ggplot2-how-to-color-a-graph-by-multiple-variables
This worked in a way but it gave me my bars in a colour and the frame in another which is not what I want
I also tried putting in the colors by hand
g<-ggplot(melt_dat, aes(x=value, y=prot_names, fill=variable))+
geom_bar(stat='identity', position='dodge')+
scale_fill_manual(values = c("red1","skyblue1","red1","red4","skyblue4","red4")
which did not work either as it only colours by condition (only uses red1 and skyblue 1 independant of value>0)
答案1
得分: 0
你可以使用嵌套的ifelse
(或if_else
),但在涉及多个条件的情况下,使用dplyr
中的case_when
会更容易。在你的情况下:
library(dplyr)
melt_dat <- melt_dat %>% mutate("type" = case_when(value > 0 & variable == "Expression_Con_A" ~ "color1",
value < 0 & variable == "Expression_Con_A" ~ "color2",
value > 0 & variable == "Expression_Con_B" ~ "color3",
value < 0 & variable == "Expression_Con_B" ~ "color4"))
ggplot(melt_dat, aes(x=value, y=prot_names, fill=type)) +
geom_bar(stat='identity', position='dodge') +
scale_fill_manual(values = c("red1","skyblue1","red1","red4","skyblue4","red4"))
我定义了一个新变量"type",请注意,在ggplot中,你需要将"fill"参数从"variable"更改为这个新变量。
英文:
You can use nested ifelse
(or if_else
), but in case of multiple conditions it'd be easier to use case_when
from dplyr, In your case:
library(dplyr)
melt_dat <- melt_dat %>% mutate("type" = case_when(value > 0 & variable == "Expression_Con_A" ~ "color1",
value < 0 & variable == "Expression_Con_A" ~ "color2",
value > 0 & variable == "Expression_Con_B" ~ "color3",
value < 0 & variable == "Expression_Con_B" ~ "color4"))
ggplot(melt_dat, aes(x=value, y=prot_names, fill=type)) +
geom_bar(stat='identity', position='dodge') +
scale_fill_manual(values = c("red1","skyblue1","red1","red4","skyblue4","red4"))
I defined a new variable "type", and note that in the ggplot you've to change the "fill" argument from "variable" to this new variable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论