R ggplot条件比例\填充\手动颜色

R ggplot条件比例\填充\手动颜色,r,ggplot2,colors,R,Ggplot2,Colors,希望根据某列中的值为我的ggplot设置两种不同的颜色方案 这是我的数据: itemName = c("item1", "item2", "item3", "item4","item5", "item1", "item2", "item3", "item4", "item5") MPindex = c(124, 118, 101, 112, 126, 124, 118, 101, 112, 126) code = c("A", "A", "A", "A", "B", "A", "A", "A"

希望根据某列中的值为我的ggplot设置两种不同的颜色方案

这是我的数据:

itemName = c("item1", "item2", "item3", "item4","item5", "item1", "item2", "item3", "item4", "item5") 
MPindex = c(124, 118, 101, 112, 126, 124, 118, 101, 112, 126)
code = c("A", "A", "A", "A", "B", "A", "A", "A", "A", "B")
topTwo = c(55.6, 53,45.2, 50.2, 56.2, 55.6, 53, 45.2, 50.2, 56.2)
variable = c(4, 4, 4, 4, 4, 5, 5, 5, 5, 5) 
value = c(37.2, 36.2, 31.4, 31.2, 34.2, 18.4, 16.8, 13.8, 19, 22)

testNew<-data.frame(itemName,MPindex,code,topTwo,variable,value)
itemName=c(“项目1”、“项目2”、“项目3”、“项目4”、“项目5”、“项目1”、“项目2”、“项目3”、“项目4”、“项目5”)
MPindex=c(124118101112126124118101112126)
代码=c(“A”、“A”、“A”、“A”、“B”、“A”、“A”、“A”、“A”、“A”、“B”)
topTwo=c(55.6,53,45.2,50.2,56.2,55.6,53,45.2,50.2,56.2)
变量=c(4,4,4,4,4,5,5,5,5)
数值=c(37.2,36.2,31.4,31.2,34.2,18.4,16.8,13.8,19,22)

testNewIt听起来像是
fill
应该基于
code
variable
的组合。然后,您可以给
缩放\u填充\u手动
指定颜色向量。它可能看起来像
values=c(“A.4”=“#33CCCC”,“A.5”=“#00A499”,“B.4”=“浅灰色”,“B.5”=“灰色”)
。如果你想要一个更具体的例子,请将你的数据集粘贴到你的问题中,而不是放一张数据集的图片。谢谢!我添加了testNew$color,您可以使用
dput()
谢谢。dput从来都不是什么新鲜事。它听起来像是
fill
应该基于
code
variable
的组合。然后,您可以给
缩放\u填充\u手动
指定颜色向量。它可能看起来像
values=c(“A.4”=“#33CCCC”,“A.5”=“#00A499”,“B.4”=“浅灰色”,“B.5”=“灰色”)
。如果你想要一个更具体的例子,请将你的数据集粘贴到你的问题中,而不是放一张数据集的图片。谢谢!我添加了testNew$color,您可以使用
dput()
谢谢。dput从来都不是什么新鲜事。
  p14 = ggplot(data = testNew, aes(x=reorder(itemName,-MPindex),y=value,fill=variable))+blank_theme+
geom_bar(stat="identity")+geom_text(aes(label=paste(value,"%")),vjust=1.2,colour='white')+theme(axis.text.x = element_text(angle = 45,hjust=1.00,vjust = 1.15))+
xlab("")+scale_fill_manual(values = c("#33CCCC","#00A499"))

blank_theme <- theme_minimal()+
  theme(
    axis.text.y = element_blank(),
    axis.title.y = element_blank(),
    panel.border = element_blank(),
    panel.grid=element_blank(),
    axis.ticks = element_blank(),
   plot.title=element_text(size=14, face="bold")
  )

p14