簡體   English   中英

在R中對齊直方圖和散點圖

[英]align histogram and scatter plot in R

我試圖顯示兩個圖,一個直方圖和一個散點圖,我希望它們相對於x軸對齊。 我用hist()創建了直方圖,用ggplot2創建了散點圖。 但是,我發現創建混合這兩者的多圖的問題,因此我決定使用ggplot2創建直方圖。 請在下面找到代碼:

loadedimage <- readTIFF("my_data05.tiff")
rasterizedimage <- rasterGrob(loadedimage, interpolate = TRUE)
realmadrid_colors <-c("#00529f", "#8cc1f3", "blue")
data_test <- subset(full_data, Rival == "Barcelona")
data_kroos <- subset(data_test, Jugador == "Kroos")

myscatter <- ggplot(data_kroos, aes(x = x1, y = y1)) + 
  annotation_custom(rasterizedimage, xmin = 0, xmax = 713, ymin = 0 , ymax = 497) +
  geom_point(aes(fill = factor(data_kroos$Resultado)),
         shape = 21, 
         size = 5) +
  scale_fill_manual(values=realmadrid_colors) + 
  guides(fill=guide_legend(title=NULL)) +
  coord_fixed() + 
  theme_bw() + 
  theme_no_axes(base.theme = theme_bw()) + 
  theme(legend.position = c(0.5, 0.04),
    legend.box = "horizontal",
    legend.direction = "horizontal",
    legend.box.background = element_rect(fill = NA, colour = "transparent"),
    legend.text = element_text(family = "Courier", size = 14),
    axis.title = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    plot.margin=unit(c(-0.05,-0.05,-0.05,-0.1),"in")
    )

myhistogram <- qplot(data_barca[data_barca$Sección == "Passes_All",]$x1, breaks=seq(0, 660, by = 66), alpha = 0.5, geom = "histogram")

如果嘗試使用grid.newpage()和grid.draw(),則會收到錯誤消息:ncol(x)== ncol(y)不是TRUE

grid.newpage()
grid.draw(rbind(ggplotGrob(myscatter), ggplotGrob(myhistogram), size = "first"))

我想在http://biokit.readthedocs.io/en/latest/auto_examples/plot_scatter.html中獲得類似的信息(實際上我仍然處於第一步,我的目標是完全像以前的鏈接一樣),或者類似的東西例如將直方圖與R中的散點圖結合起來 ,但是在那種情況下,它不會被ggplot2使用,而且我不確定是否可以像使用該程序包一樣玩轉。

歡迎任何幫助! 如果您需要我上傳任何文件,請告訴我。

提前致謝!

進一步的搜索可能是我的答案! 我要查找的詞是邊際直方圖!

ggplot2中具有邊緣直方圖的散點圖

暫無
暫無

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

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