如何在R中制作双Y轴杆图(道奇)?

发布于 2025-01-22 02:41:27 字数 627 浏览 0 评论 0原文

我正在尝试在单X轴上制作双杆图,如何执行它?我知道如何处理线路。例如,我正在使用mtcars数据。我也想在该条中添加传奇和错误行。

library(ggplot2)

scaleFactor <- max(mtcars$cyl) / max(mtcars$hp)

ggplot(mtcars, aes(x=disp)) +
  geom_smooth(aes(y=cyl), method="loess", col="blue") +
  geom_smooth(aes(y=hp * scaleFactor), method="loess", col="red") +
  scale_y_continuous(name="cyl", sec.axis=sec_axis(~./scaleFactor, name="hp")) +
  theme(
    axis.title.y.left=element_text(color="blue"),
    axis.text.y.left=element_text(color="blue"),
    axis.title.y.right=element_text(color="red"),
    axis.text.y.right=element_text(color="red")
  )

I am trying to make dual bar plot in single X axis, how to perform it? I know how to do with line plot. for example I am using mtcars data. Also i want to add legends and error line in that bars.

library(ggplot2)

scaleFactor <- max(mtcars$cyl) / max(mtcars$hp)

ggplot(mtcars, aes(x=disp)) +
  geom_smooth(aes(y=cyl), method="loess", col="blue") +
  geom_smooth(aes(y=hp * scaleFactor), method="loess", col="red") +
  scale_y_continuous(name="cyl", sec.axis=sec_axis(~./scaleFactor, name="hp")) +
  theme(
    axis.title.y.left=element_text(color="blue"),
    axis.text.y.left=element_text(color="blue"),
    axis.title.y.right=element_text(color="red"),
    axis.text.y.right=element_text(color="red")
  )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

错々过的事 2025-01-29 02:41:27

需要重组数据,以便与GGPLOT正确运行。

这是我如何以三列和mtcars行的示例来完成的:

# Only use first 3 rows and columns
mtcarsmod <- mtcars[1:3, 1:3]

# Restructure data
Car <- rownames(mtcarsmod)
Part <- colnames(mtcarsmod)
Value <- as.vector(t(mtcarsmod))
mtcarsmod <- data.frame("Car"=rep(Car,each=length(Part)), "Part"=rep(Part, times=length(Car)), "Value"= Value)


# Visualize data
library(ggplot2)

# Have Cars on X axis
plot <- ggplot(mtcarsmod, aes(x=Car, y=Value, fill=Part)) +   
               geom_bar(position="dodge", stat="identity")
plot

# Have Variables on X axis
plot <- ggplot(mtcarsmod, aes(x=Part, y=Value, fill=Car)) +   
               geom_bar(position="dodge", stat="identity")
plot


# Have Cars on Y axis
plot <- ggplot(mtcarsmod, aes(x=Value, y=Car, fill=Part)) +   
              geom_bar(position="dodge", stat="identity")
plot

# Have Variables on Y axis
plot <- ggplot(mtcarsmod, aes(x=Value, y=Part, fill=Car)) +   
              geom_bar(position="dodge", stat="identity")
plot

the data would need to be restructured, in order to function properly with ggplot.

Here is how I did it with the example of three columns and rows of mtcars:

# Only use first 3 rows and columns
mtcarsmod <- mtcars[1:3, 1:3]

# Restructure data
Car <- rownames(mtcarsmod)
Part <- colnames(mtcarsmod)
Value <- as.vector(t(mtcarsmod))
mtcarsmod <- data.frame("Car"=rep(Car,each=length(Part)), "Part"=rep(Part, times=length(Car)), "Value"= Value)


# Visualize data
library(ggplot2)

# Have Cars on X axis
plot <- ggplot(mtcarsmod, aes(x=Car, y=Value, fill=Part)) +   
               geom_bar(position="dodge", stat="identity")
plot

# Have Variables on X axis
plot <- ggplot(mtcarsmod, aes(x=Part, y=Value, fill=Car)) +   
               geom_bar(position="dodge", stat="identity")
plot


# Have Cars on Y axis
plot <- ggplot(mtcarsmod, aes(x=Value, y=Car, fill=Part)) +   
              geom_bar(position="dodge", stat="identity")
plot

# Have Variables on Y axis
plot <- ggplot(mtcarsmod, aes(x=Value, y=Part, fill=Car)) +   
              geom_bar(position="dodge", stat="identity")
plot
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文