Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-绘制基线不为';不要从零开始_R_Ggplot2 - Fatal编程技术网

R-绘制基线不为';不要从零开始

R-绘制基线不为';不要从零开始,r,ggplot2,R,Ggplot2,我正在使用ggplot2中的geom_bar在减淡条形图中绘制8种条件的平均值。我的y轴从-200到1000。8个条件中有两个为负值,其余为正值。当前正值的条形图从0开始。我希望所有的条从-200开始(即从-200填充到各自的值)。有没有一个简单的方法来实现这一点 graph <- ggplot(data=data, aes(x, y, fill = z)) + geom_bar(stat="identity", position = "dodge", width = 0.

我正在使用ggplot2中的geom_bar在减淡条形图中绘制8种条件的平均值。我的y轴从-200到1000。8个条件中有两个为负值,其余为正值。当前正值的条形图从0开始。我希望所有的条从-200开始(即从-200填充到各自的值)。有没有一个简单的方法来实现这一点

graph <-  ggplot(data=data, aes(x, y, fill = z)) + 
      geom_bar(stat="identity", position = "dodge", width = 0.4) +
      scale_y_continuous(limits = c(-200, 1000), breaks =c(-200,0,200,400,600,800,1000), expand = c(0,0))

graph对于基线为-200而不是零的条形图,可以将200添加到y值,然后重新标记y轴,使标签比“默认”标签小200。你还没有提供样本数据,所以如果这不是你想要的,请告诉我

# Fake data (adapted from @lawyeR)
x <- rep(c("AA", "BB", "CC", "DD"), 2) 
y <- c(4, 423, -57, 724, 14, 556, 37, 614) 
z <- rep(c("A", "B", "C", "D"), 2) 
data <- data.frame(x=factor(x), y=y, z=z) 

library(dplyr)

# Add a grouping variable for dodging multiple bars with the same x value
data = data %>% group_by(x) %>%
  arrange(y) %>%
  mutate(dodgeGroup = 1:n())

ggplot(data=data, aes(x, y + 200, fill = z, group=dodgeGroup)) + 
  geom_bar(stat="identity", position = "dodge", width = 0.5,
           colour="grey20", lwd=0.3) + 
  scale_y_continuous(limits = c(-200,1000), breaks=seq(-200,1000,200),
                     labels=seq(-200,1000,200) - 200)
#伪造数据(改编自@lawyeR)

这里有一个简单的方法。x