ggplot在x轴上显示表达式

时间:2016-06-10 22:35:51

标签: r ggplot2 expression bar-chart

下面,我有R代码绘制分组条形图。

<div class="main" id="clicker">
  Expand
</div>
<div class="secondary" id="expandable">
  number1, <br> number2, <br> number3, <br> number4.
</div>

<script>
 $( document ).ready(function() {

$(".secondary").hide();

$(document).ready(
    function(){
        $("#clicker").click(function () {
            $(".secondary").toggle("slow");
        });

    });   

});
</script>

目前,情节如下图所示。但是,我想在x轴上显示表达式,如下图所示。

enter image description here

我试过这个:

group_name = c('A_1x', 'A_1x', 'A_2x', 'A_2x', 'A_3x', 'A_3x', 'A_4x', 'A_4x')

mydata2 <- data.frame(mygroup = group_name, 
                      mysubgroup = factor(c("Yes", "No"), 
                                          levels = c("Yes", "No")), 
                      value = c(60,40,90,10,55,45,88,12))


ggplot(mydata2, aes(mygroup, value, fill = mysubgroup)) + 
  geom_bar(position = "dodge", width = 0.5, stat = "identity")+ 
  coord_flip() 

但它会出现以下错误:

group_name = c(expression(A[1*x]),expression(A[1*x]),
               expression(A[2*x]),expression(A[2*x]),
               expression(A[3*x]),expression(A[3*x]),
               expression(A[4*x]),expression(A[4*x]))

如何解决?

3 个答案:

答案 0 :(得分:5)

这是一个工作示例 - 我将group_name更改为4个元素而不是8个,并手动将它们添加到ggplot表达式中。问题是表达式类型不能是data.frame的列名。这避免了这个问题。

library(ggplot2)
group_name = c('A_1x', 'A_1x', 'A_2x', 'A_2x', 'A_3x', 'A_3x', 'A_4x', 'A_4x')
mydata2 <- data.frame(mygroup = group_name, 
                      mysubgroup = factor(c("Yes", "No"), 
                                          levels = c("Yes", "No")), 
                      value = c(60,40,90,10,55,45,88,12))

group_name = c(expression(A[1*x]),
               expression(A[2*x]),
               expression(A[3*x]),
               expression(A[4*x]))

ggplot(mydata2, aes(mygroup, value, fill = mysubgroup)) + 
  geom_bar(position = "dodge", width = 0.5, stat = "identity")+ 
  coord_flip() +
  scale_x_discrete(labels=group_name)  # Adding the labels here 

答案 1 :(得分:3)

labels可以是一个函数,

group_name = sprintf("A[%i*x]", rep(1:4,each=2))

# alternatively, use gsub with your original vector
# group_name = c('A_1x', 'A_1x', 'A_2x', 'A_2x', 'A_3x', 'A_3x', 'A_4x', 'A_4x')
# gsub("A_([0-9])x","A[\\1*x]", group_name)

mydata2 <- data.frame(mygroup = group_name, 
                      mysubgroup = factor(c("Yes", "No"), 
                                          levels = c("Yes", "No")), 
                      value = c(60,40,90,10,55,45,88,12))


ggplot(mydata2, aes(mygroup, value, fill = mysubgroup)) + 
  geom_bar(position = "dodge", width = 0.5, stat = "identity")+ 
  coord_flip() + scale_x_discrete(labels = function(l) parse(text=l))

enter image description here

答案 2 :(得分:1)

@baptiste建议使用函数是很好的,尽管他的代码可能不完整以给出预期的结果。也可以直接使用parse

中的scale_x_discrete(labels = parse(text=levels(mydata2$mygroup)))函数

修改代码以生成图:

group_name = sprintf("A[%i*x]", rep(1:4,each=2))

# alternatively, use gsub with your original vector
# group_name = c('A_1x', 'A_1x', 'A_2x', 'A_2x', 'A_3x', 'A_3x', 'A_4x', 'A_4x')
# gsub("A_([0-9])x","A[\\1*x]", group_name)

mydata2 <- data.frame(mygroup = group_name, 
                  mysubgroup = factor(c("Yes", "No"), 
                                      levels = c("Yes", "No")), 
                  value = c(60,40,90,10,55,45,88,12))

ggplot(mydata2, aes(mygroup, value, fill = mysubgroup)) + 
  geom_bar(position = "dodge", width = 0.5, stat = "identity")+ 
  coord_flip() + 
  scale_x_discrete(labels = parse(text=levels(mydata2$mygroup)))