Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:17:20+00:00 2026-06-10T03:17:20+00:00

I am trying to do some density plots in R. I originally used density

  • 0

I am trying to do some density plots in R. I originally used density plot but I changed to the density plot in ggplot2 because I visually prefer ggplot2.

So I did a density plot using the density plot function and did a density plot in ggplot2 (see below) but I found the plots were not identical. It looks like some of the y-values have been lost or dropped in the ggplot2 (right plot). Is there any particular reason for this? How can I make the ggplot identical to the destiny plot (left plot).

enter image description here

Code:

library(ggplot2)
library(grid)
par(mfrow=c(1,2))

# define function to create multi-plot setup (nrow, ncol)
vp.setup <- function(x,y){
 grid.newpage()
 pushViewport(viewport(layout = grid.layout(x,y)))
}

# define function to easily access layout (row, col)
vp.layout <- function(x,y){
  viewport(layout.pos.row=x, layout.pos.col=y)
}


vp.setup(1,2)

dat <- read.table(textConnection("
low high
10611.0 14195.0
10759.0 14437.0
10807.0 14574.0
10714.0 14380.0
10768.0 14448.0
10601.0 14239.0
10579.0 14218.0
10806.0 14510.0
"), header=TRUE, sep="\t")

plot(density(dat$low))

dat.low = data.frame(low2 = c(dat$low), lines = rep(c("low")))

low_plot_gg = (ggplot(dat.low, aes(x = low2, fill = lines)) +
 stat_density(aes(y = ..density..)) + 
 coord_cartesian(xlim = c(10300, 11000)) 
)
print(low_plot_gg, vp=vp.layout(1,2))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:17:22+00:00Added an answer on June 10, 2026 at 3:17 am

    Based on some trial and error, it looks like you want

     + xlim(c(10300,11000))
    

    rather than

     + coord_cartesian(xlim = c(10300, 11000)) 
    

    coord_cartesian extends the limits of the plots but doesn’t change what’s drawn inside them at all …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying some code to open the iPod Library using the simulator but
I am trying to make a 2D density plot (from some simulation data) with
I am a newbie at jQuery. I was trying some things but I need
I'm using Eclipse with C++ plugins on my macbook, trying some practice projects to
I am trying some SQL code but I get an error when I try
I am trying some DB based apps in iphone.I used this sample as a
I'm currently trying to plot a histogram with an overlay (given by my_fun) using
I was trying some C++, but I'm too new to this and you can
I am trying to perform a multivariate test for normality on some density data
I'm trying some string manipulation in my web app, but I don't want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.