簡體   English   中英

如何使用數據框中的不同列在一個繪圖上繪制多個圖形?

[英]How to plot multiple graphs on one plot using a different columns in a dataframe?

我有一個包含4列的數據框,我想使用qplot繪制第1列對第2列和第3列對第4列,並將所有圖繪制在同一張圖上,我需要將此擴展為20列的數據框。 謝謝你提供的所有幫助

編輯下面是我正在使用的數據框的示例:

            1         2          3          4
1  0.01795918 0.9755562 0.02040816 0.05259072
2  0.04244898 0.9455753 0.03591837 0.03864464
3  0.05224490 0.9816900 0.06122449 0.03280435
4  0.07183673 0.9635419 0.08000000 0.03453257
5  0.09551020 0.9821122 0.10040816 0.03134642
6  0.12000000 0.9354895 0.11510204 0.03920271
7  0.13877551 0.9703654 0.13877551 0.03588973
8  0.16244898 0.9506424 0.15836735 0.03402917
9  0.17224490 0.9610043 0.18530612 0.03621932
10 0.20000000 0.9863483 0.19591837 0.03021983
11 0.22122449 0.9845782 0.22530612 0.03268187
12 0.22938776 0.9835922 0.22530612 0.03513692
....

我可以將一個相對於另一個繪制,但是我需要一種將它們全部繪制到一個圖形上的方法

場景:1

比較在多個類別中的許多個體上測得的單個變量。 在這種情況下,我提供了幾種單變量可視化並將它們繪制在單個頁面上。

 library(ggplot2) attach(iris) plot_1 = ggplot(iris, aes(x=Petal.Length, colour=Species)) + geom_density() + labs(title="Density plots") plot_2 = ggplot(iris, aes(x=Petal.Length, fill=Species)) + geom_histogram(colour="grey30", binwidth=0.15) + facet_grid(Species ~ .) + labs(title="Histograms") plot_3 = ggplot(iris, aes(y=Petal.Length, x=Species)) + geom_point(aes(colour=Species), position=position_jitter(width=0.05, height=0.05)) + geom_boxplot(fill=NA, outlier.colour=NA) + labs(title="Boxplots") plot_4 = ggplot(iris, aes(y=Petal.Length, x=Species, fill=Species)) + geom_dotplot(binaxis="y", stackdir="center", binwidth=0.15) + labs(title="Dot plots") library(gridExtra) part_1 = arrangeGrob(plot_1, plot_2, heights=c(0.4, 0.6)) part_2 = arrangeGrob(plot_3, plot_4, nrow=2) parts_12 = arrangeGrob(part_1, part_2, ncol=2, widths=c(0.6, 0.4)) # To save the plots ggsave(file="figures/plots.png", parts_12, height=6, width=10, units="in") 

同一頁面上的多個圖

場景2

另一個角度可能是在同一頁面上混合多個圖形。 我在下面顯示;

 # Libraries required library(ggpubr) # Data: ToothGrowth and mtcars data sets. # ToothGrowth data("ToothGrowth") head(ToothGrowth) # mtcars data("mtcars") head(mtcars) mtcars$name <- rownames(mtcars) # add column name mtcars$cyl <- as.factor(mtcars$cyl) head(mtcars[, c("name", "wt", "mpg", "cyl")]) # create some plots # Box plots and dot plots using the ToothGrowth data set # Box plot bxp<- ggboxplot(data = ToothGrowth, x="dose", y="len", color = "dose", palette = "jco") bxp # Dot plot dp<- ggdotplot(data = ToothGrowth, x="dose", y="len", color = "dose", palette = "jco", binwidth = 1) dp # Bar plots and scatter plots using the mtcars data set # Create an ordered bar plot by changing the fill color by the grouping variable “cyl”. Sorting will be done globally, but not by groups. bp <- ggbarplot(mtcars, x = "name", y = "mpg", fill = "cyl", # change fill color by cyl color = "white", # Set bar border colors to white palette = "jco", # jco journal color palett. see ?ggpar sort.val = "asc", # Sort the value in ascending order sort.by.groups = TRUE, # Sort inside each group x.text.angle = 90 # Rotate vertically x axis texts ) bp + font("x.text", size = 8) # Scatter plots (sp) sp <- ggscatter(mtcars, x = "wt", y = "mpg", add = "reg.line", # Add regression line conf.int = TRUE, # Add confidence interval color = "cyl", palette = "jco", # Color by groups "cyl" shape = "cyl" # Change point shape by groups "cyl" )+ stat_cor(aes(color = cyl), label.x = 3) # Add correlation coefficient sp # Arrange on one page # We will use the ggarrange() [in ggpubr] ggarrange(bxp, dp, bp + rremove("x.text"), labels = c("A", "B", "C"), ncol = 2, nrow = 2) # Alternatively, you can also use the function grid.arrange()[in gridExtra] # Annotate the arranged figure R function: annotate_figure() [in ggpubr] figure <- ggarrange(sp, bp + font("x.text", size = 10), ncol = 1, nrow = 2) annotate_figure(figure, top = text_grob("Visualizing mpg", color = "red", face = "bold", size = 14), bottom = text_grob("Data source: \\n mtcars data set", color = "blue", hjust = 1, x = 1, face = "italic", size = 10), left = text_grob("Figure arranged using ggpubr", color = "green", rot = 90), right = "I'm done, thanks :-)!", fig.lab = "Figure 1", fig.lab.face = "bold" ) # Change column/row span of a plot # We'll use nested ggarrange() functions to change column/row span of plots. ggarrange(sp, # First row with scatter plot ggarrange(bxp, dp, ncol = 2, labels = c("B", "C")), # Second row with box and dot plots nrow = 2, labels = "A" # Labels of the scatter plot ) 

多圖的另一個例子

您可以使用基本繪圖系統,在繪圖之前先分配所需的圖形數量:

par(mfrow = c(1,2)

這等於1x2的繪圖圖

我不知道您的數據框是什么樣子,但最簡單的方法是進行如下操作: set.seed(123) df <- data.frame(x = rnorm(100), y = rnorm(100), z = rnorm(100), c = rnorm(100)) plot(df$x, df$y) plot(df$z, df$c)

可復制的例子

df <- data.frame(A=runif(20), B=runif(20), C=runif(20), D=runif(20))

一些數據准備

將數據重組到

library(dplyr)
library(tidyr)
df1 <- df %>%
         gather(key, value) %>%                                       # convert everything into long format
         mutate(grp = rep(1:(ncol(df)/2), each=(nrow(df)*2))) %>%     # Each pairs of columns gets unique grouping value
         mutate(index = rep(1:nrow(df), ncol(df))) %>%                # Each observation in each group gets a unique value
         mutate(key = rep(rep(c("x","y"), each=nrow(df)), ncol(df)/2)) %>%      # label as x and y
         spread(key, value)                                           # convert to wide format again

   grp index         x          y
1    1     1 0.4820801 0.47761962
2    1     2 0.5995658 0.86120948
3    1     3 0.4935413 0.43809711
4    1     4 0.1862176 0.24479728
5    1     5 0.8273733 0.07067905
# etc

ggplot基本解決方案

使用facet_wrap通過N grp繪制N個

library(ggplot2)
ggplot(data=df1, aes(x=x, y=y)) + 
  geom_point() +
  facet_wrap(~grp)

使用geom_smooth在一個圖中繪制所有數據

ggplot(data=df1, aes(x=x, y=y, colour=factor(grp))) + 
  geom_smooth()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM