颜色单个ggplot轴项目 [英] colour single ggplot axis item

查看:118
本文介绍了颜色单个ggplot轴项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个图表,并且想要根据变量为其中一个x轴项目着色。我看过这篇文章(如何在ggplot图形的单个坐标轴上使用不同颜色的坐标轴标签?),但我正努力将其应用于我的数据集。



<$ p $ (a,b,c,a,b,c,a,b ,c),
val = c(99,120,79,22,43,53,12,27,31),
type = c(alpha,alpha,alpha , 喝彩, 喝彩, 喝彩, 查理, 查理, 查理))

MYVAR = 一个

ggplot( df1,aes(x = reorder(var,-val),y = val,fill = type))+ geom_bar(stat =identity)

当x轴值等于 myvar ?$ b $时, b

更新:感谢@ddiez提供了一些指导。我终于想到了在绘图之前我必须重新排序的事实。我也应该用data.table做出我最初的例子,所以我不确定这是否会影响原始响应。我将原始数据集修改为data.table,并使用以下代码获得成功。

  df1 < -  data。 table(var = c(a,b,c,a,b,c,a,b,c),
val = c (99,120,79,22,43,53,12,27,31),
type = c(alpha,alpha,alpha,bravo,bravo,bravo, 查理, 查理, 查理))

MYVAR = 一个

DF1 [,axisColour:= ifelse(VAR == MYVAR, 红 (df1,var,type)

ggplot(df1,aes(x = var,y = val,fill = type))+ geom_bar(stat =identity)+
theme(axis.text.x = element_text(color = df1 [ ,axisColour [1],by = var] [,V1]))

解决方案

可能有一个更优雅的解决方案,但一个快速破解(要求你知道最终的订单)将是:
$ b $ pre $ ggplot(df1 ,AE s(x = reorder(var,-val),y = val,fill = type))+
geom_bar(stat =identity)+
theme(axis.text.x = element_text(color = c(黑色,黑色,红色)))

变量 myvar (但是,可能有更好的方法):

 #重新排列data.frame中的因素(而不是原位)。 
df1 $ var = reorder(df1 $ var,-df1 $ val)

#为每个关卡创建一个颜色向量。
mycol = rep(black,nlevels(df1 $ var))
名称(mycol)= levels(df1 $ var)

#赋予所需的不同颜色。
mycol [MYVAR] = 红色

ggplot(DF1,AES(X = VAR,Y =缬氨酸,填=类型))+
geom_bar(STAT =同一性)+
主题(axis.text.x = element_text(color = mycol))


I have created a chart and am wanting to colour one of the x-axis items based on a variable. I have seen this post (How to get axis ticks labels with different colors within a single axis for a ggplot graph?), but am struggling to apply it to my dataset.

df1 <- data.frame(var=c("a","b","c","a","b","c","a","b","c"), 
                  val=c(99,120,79,22,43,53,12,27,31),
                  type=c("alpha","alpha","alpha","bravo","bravo","bravo","charlie","charlie","charlie"))

myvar="a"

ggplot(df1,aes(x=reorder(var,-val), y=val,fill=type)) + geom_bar(stat="identity")

Any tips on how to make the x-axis value red when it is equal to myvar?

Update: Thanks to @ddiez for some guidance. I finally came around to the fact that i would have to reorder prior to plotting. I also should have made my original example with data.table, so am not sure if this would influenced original responses. I modified my original dataset to be a data.table and used the following code to achieve success.

df1 <- data.table(var=c("a","b","c","a","b","c","a","b","c"), 
                  val=c(99,120,79,22,43,53,12,27,31),
                  type=c("alpha","alpha","alpha","bravo","bravo","bravo","charlie","charlie","charlie"))

myvar="a"

df1[,axisColour := ifelse(var==myvar,"red","black")]
df1$var <- reorder(df1$var,-df1$val,sum)

setkey(df1,var,type)

ggplot(df1,aes(x=var, y=val,fill=type)) + geom_bar(stat="identity") +
  theme(axis.text.x = element_text(colour=df1[,axisColour[1],by=var][,V1]))

解决方案

There may be a more elegant solution but a quick hack (requires you to know the final order) would be:

ggplot(df1,aes(x=reorder(var,-val), y=val,fill=type)) +
geom_bar(stat="identity") +
theme(axis.text.x = element_text(colour=c("black","black","red")))

A solution using the variable myvar (yet, there may be better ways):

# reorder the factors in the data.frame (instead of in situ).
df1$var=reorder(df1$var, -df1$val)

# create a vector of colors for each level.
mycol=rep("black", nlevels(df1$var))
names(mycol)=levels(df1$var)

# assign the desired ones a different color.
mycol[myvar]="red"

ggplot(df1,aes(x=var, y=val,fill=type)) +
  geom_bar(stat="identity") +
  theme(axis.text.x = element_text(colour=mycol))

这篇关于颜色单个ggplot轴项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆