条形图ggplot2 R上的折线图

时间:2020-09-20 02:09:25

标签: r ggplot2 plot dplyr

我正在尝试使用$domain = $_SERVER['HTTP_HOST']; $secure = TRUE; $httponly = TRUE; $samesite = 'lax'; if ( PHP_VERSION_ID < 70300 ) { session_set_cookie_params([0, '/; samesite='.$samesite, $domain, $secure, $httponly]); } else { session_set_cookie_params([ 'lifetime' => 0, 'path' => '/', 'domain' => $domain, 'secure' => $secure, 'httponly' => $httponly, 'samesite' => $samesite ]); } session_start(); line graph上将bar plot绘制在double-y-axis上。

以下是条形图的示例数据和代码:

ggplot2

我正在尝试将library(ggplot2) data <- data.frame("groups" = c('AA','BB','CC', 'AA','BB','CC', 'AA','BB','CC'), "something" = c('aaaaaa', 'bbbbb', 'cccccc', 'dddddddd', 'eeeeeee', 'ffffffff', 'gggggggggg', 'hhhhhhhhh', 'iiiiiiiii'), "value1" = c(1.1, 2.4, 3.5, 5, 4.1, 3, 2.5, 1.4, 4.5), "value2" = c(2, 25, 10, 4, 15, 5, 3, 4, 8)) p1 <- ggplot(data, aes(x = something, y = value1, fill = groups)) + geom_bar(stat = "identity", position = "dodge") + xlab("something") + ylab("groups") + labs(fill = "groups") + theme_bw() + theme(aspect.ratio = 2/1, axis.title.x = element_text(face="bold"), axis.title.y = element_text(face="bold"), axis.text.x = element_text(angle = 90, vjust = 0.2, hjust=1)) + coord_equal(1/0.2) 添加到上面的line中。但是,bar chart两侧的scale显示相同。

y-axis

当我尝试操纵biostats所示的p1 + geom_line(aes(x = something, y = value2), size = 1, color="red", group = 1) + scale_y_continuous(sec.axis = sec_axis(~., name = "Count")) + geom_point(aes(x = something, y = value2), size = 1, color="blue", group = 1) + theme(axis.line.y.right = element_line(color = "red"), axis.ticks.y.right = element_line(color = "red"), axis.text.y.right = element_text(color = "red"), axis.title.y.right = element_text(color = "red")) 值时。但是,我仍然没有获得正确的ylinebars

values

我想为每个对应的p1 + geom_line(aes(x = something, y = 0.1*value2), size = 1, color="red", group = 1) + scale_y_continuous(sec.axis = sec_axis(~./0.1, name = "Count")) + geom_point(aes(x = something, y = 0.1*value2), size = 1, color="blue", group = 1) + theme(axis.line.y.right = element_line(color = "red"), axis.ticks.y.right = element_line(color = "red"), axis.text.y.right = element_text(color = "red"), axis.title.y.right = element_text(color = "red")) 建立正确的linebar图。

2 个答案:

答案 0 :(得分:2)

在您的情节上尝试这些更改。似乎是变量比例因子存在问题:

library(ggplot2)
#Data
data <- data.frame("groups" = c('AA','BB','CC', 'AA','BB','CC', 'AA','BB','CC'), 
                   "something" = c('aaaaaa', 'bbbbb', 'cccccc', 'dddddddd', 'eeeeeee', 'ffffffff', 'gggggggggg', 'hhhhhhhhh', 'iiiiiiiii'),
                   "value1" = c(1.1, 2.4, 3.5, 5, 4.1, 3, 2.5, 1.4, 4.5),
                   "value2" = c(2, 25, 10, 4, 15, 5, 3, 4, 8))
#Scale factor
sf <- max(data$value1)/max(data$value2)
#Plot
ggplot(data, aes(x = something,
                       y = value1,
                       fill = groups)) + geom_bar(stat = "identity",  position = "dodge") + 
  xlab("something") + ylab("groups") + labs(fill = "groups") +
  theme_bw() + theme(aspect.ratio = 2/1,
                     axis.title.x = element_text(face="bold"),
                     axis.title.y = element_text(face="bold"),
                     axis.text.x = element_text(angle = 90, vjust = 0.2, hjust=1)) + 
  coord_equal(1/sf)+
  geom_line(aes(x = something, y = value2*sf), size = 1, color="red", group = 1) +
  scale_y_continuous(sec.axis = sec_axis(~./sf, name = "Count")) +
  geom_point(aes(x = something, y = value2*sf), size = 1, color="blue", group = 1) +
  theme(axis.line.y.right = element_line(color = "red"),
        axis.ticks.y.right = element_line(color = "red"),
        axis.text.y.right = element_text(color = "red"),
        axis.title.y.right = element_text(color = "red"))

输出:

enter image description here

代码中的原始图是下一个:

enter image description here

答案 1 :(得分:1)

引入第二根轴存在两个问题。第一个比较笼统。双轴图表上的刻度是任意的,导致读者对两次测量的关系做出潜在的错误假设。

但是,如果您想继续使用双轴图,则第二个问题是在Ggplot2中。 sec.axis()不会构建一个全新的轴,而是基于另一个轴来变换/构建该轴。在您的情况下,第二个比例尺是第一个比例尺的5倍,因此第二个轴需要乘以5。要调整第二个图形(折线图)的范围,我们需要将其除以5。

此代码:

ggplot(data, aes(x = something,
                 y = value1,
                 fill = groups)) + 
        geom_bar(stat = "identity",  position = "dodge") + 
        geom_line(aes(x = something, y = value2/5), size = 1, color="red", group = 1) +
        geom_point(aes(x = something, y = value2/5), size = 1, color="blue", group = 1) +
        scale_y_continuous(sec.axis = sec_axis(~.*5, name = "Count")) +
        scale_x_discrete(labels = c("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii"), ,
                         guide = guide_axis(n.dodge = 2)) +
        labs(title = "Overlay of bar and lineplot",
             x = "something", 
             y = "groups", 
             fill = "groups") +
        theme_bw() + 
        theme(aspect.ratio = 2/1,
              axis.title.x = element_text(face="bold"),
              axis.title.y = element_text(face="bold"),
              axis.line.y.right = element_line(color = "red"),
              axis.ticks.y.right = element_line(color = "red"),
              axis.text.y.right = element_text(color = "red"),
              axis.title.y.right = element_text(color = "red"))

产生此plot