R语言中如何实现条形图叠加散点图?par(fig)绘图叠加异常问题求助
Hey there! Let's work through fixing your overlaid scatter plot and bar plot issue in R — it's a common hurdle for new users, so don't worry about missing details earlier.
First, let's break down what was going wrong in your original code:
- The
figparameter follows the orderc(x_left, x_right, y_bottom, y_top)(all values range from 0 to 1, where 0 is left/bottom and 1 is right/top). Your originalfig=c(1,0,...)reversed the x-axis bounds, which threw off the entire plot positioning. - Calling
dev.off()right after creating the histogram closed the graphics device, meaning all your subsequentpar()settings weren't applied to the plot you were actually trying to build. - The
space=5inbarplot()added massive gaps between bars, making the density plot look disconnected and misaligned. - Margin (
mar) settings andnew=TRUEplacement weren't tuned to prevent overlap between the two plots.
Here's the corrected code, with comments explaining each key change:
# Generate your sample data y1 <- rnorm(1000, mean=0, sd=1) y2 <- rnorm(1000, mean=0, sd=1) # Calculate histogram density data without plotting immediately # Using plot=FALSE avoids creating an extra plot we don't need y_hist <- hist(y1, freq=FALSE, breaks=100, plot=FALSE) # Set up the scatter plot first (occupies the bottom 75% of the figure) # mar=c(bottom, left, top, right) adjusts margins to avoid overlap par(fig=c(0, 1, 0, 0.75), mar=c(4, 4, 1, 1), font.axis=2, cex.axis=1.1) plot(y1, y2, pch=21, bg="red", xlim=c(-3,3), ylim=c(-3,3), xlab="Hodnoty y1", ylab="Hodnoty y2") # Added axis labels for clarity # Now overlay the bar plot on the top 25% of the figure # new=TRUE tells R to add to the existing device instead of creating a new one par(fig=c(0, 1, 0.75, 1), mar=c(0, 4, 3, 1), new=TRUE) barplot(y_hist$density, axes=FALSE, horiz=FALSE, ylim=c(0,0.4), space=0, ann=FALSE) # Add a y-axis for the density plot (optional but helpful for readability) axis(side=2, font.axis=2, cex.axis=1.1) mtext("Hustota", side=2, line=2.5, font=2) # Add title and subtitle at the top of the figure title("Hustota normálního rozdělení", side=3, line=1, font.main=2, cex.main=1.1) mtext("(generováno 1000 čísel s parametry mean=0, sd=1)", side=3, line=0, font.sub=2, cex.sub=1.1)
Key Fixes Explained:
- Skipped
dev.off(): Instead of plotting the histogram right away, we usedplot=FALSEto just capture the density data. This keeps the graphics device open for our combined plot. - Corrected
figcoordinates: The scatter plot usesc(0,1,0,0.75)(spans full width, bottom 0 to 0.75), and the bar plot usesc(0,1,0.75,1)(full width, top 0.75 to 1) — no reversed axes, so plots stack correctly. - Tweaked margins: The scatter plot has a small top margin, and the bar plot has no bottom margin, so they sit flush without overlapping.
- Reduced bar spacing:
space=0makes bars adjacent, matching the histogram's bin layout so the density plot makes visual sense. - Fixed subtitle details: Adjusted the parameter description to match your
rnorm()call (you hadsigma=0, -1but usedmean=0, sd=1).
This should give you a clean, non-overlapping plot where the scatter plot sits below and the density bar plot sits neatly on top.
内容的提问来源于stack exchange,提问作者Hana Liškařová
相关产品推荐
相关产品推荐

