Barplot自定义(添加两组标签,次要网格线)

时间:2016-05-25 11:08:51

标签: r ggplot2

我有包含四个数值的向量,并带有此值和代码:

vector <- c(9, 10, 6, 5, 5)
graf <- barplot(vector, col="red", 
                names.arg=c("Leadership ","Management",
                            "Problem Solving ", "Decision Making",
                            "Social Skills"))

text(graf, 0.5, labels=vector)

我创建了这个条形图

enter image description here

我希望的输出:  我想创建(或自定义** x ax文本位置和组合栏由PART1和PART2和alo添加grindlines **)barplot,如下图所示:

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以在names参数中添加新行字符\ n:

edit_Text.setOnFocusChangeListener(new OnFocusChangeListener() {
  @Override
  public void onFocusChange(View v, boolean hasFocus) {
      if(hasFocus){
         Drawable drawable = R.drawable.image; //Your drawable image
         drawable = DrawableCompat.wrap(drawable);
         DrawableCompat.setTint(drawable, Color.GREEN); // Set whatever color you want
         DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);
         editText.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
      }else { 
     }
    }
  });

或使用Names <- c("Leadership", "Management\n", "Problem Solving", "Decision Making\n", "Social Skills") barplot(vector, col=2, names.arg = Names) 功能:

axis()

或者创建一个函数,根据奇数或偶数设置轴的行:

b <- barplot(vector, col=2, names.arg = "")
axis(1, at= b, Names, line= 1, lty= 0) 

最后的情节,使用Names <- c("Leadership","Management", "Problem Solving", "Decision Making", "Social Skills") b <- barplot(vector, col= 2, names.arg= "") # function foo <- function(x, y){ if(x %% 2 == 0) LINE= y[1] if(x %% 2 != 0) LINE= y[2] axis(1, at=b[x, ], Names[x], line= LINE, lty= 0) } sapply(1:length(vector), foo, c(0, 1)) rectabline。您必须自己设置位置:

text

enter image description here

或使用ggplot2:

b <- barplot(vector, col= 2, names.arg= "")
abline(h= 0:10, lty= 2)
b <- barplot(vector, col= 2, names.arg= "", add= TRUE)

abline(v= 3.7, col= "blue")
rect(0, 10.5, 3.65, 11.5, col= "blue", xpd= TRUE) #xpd enables plotting outside the plot region
rect(3.76, 10.5, 6.25, 11.5, col= "blue", xpd= TRUE)
text(b[2,] ,11, "PART 1",col= "white", adj= 0.5, xpd= TRUE)
text(5, 11, "PART 2",col= "white", adj= 0.5, xpd= TRUE)

sapply(1:length(vector), foo, c(0, 1))

enter image description here