# Plot truncates on x-axis - advice to avoid this

#DENSITY PLOT TO GET RID OF LINE AT BOTTEM

``````ggplot() +
geom_density(aes(x=Y), colour="red", data=data) +
geom_line(linetype="longdash") +
geom_density(aes(x=dv,col=Y), colour="blue", data=data) +
geom_line(linetype="solid") +
scale_y_continuous(limits = c(0,NA),expand=c(0,0)) +
scale_x_continuous(limits = c(0,NA),expand=c(0,0)) +
xlab("Concerta Peak1 Cmax Distribution") +
ylab("Density")
``````

The code runs okay for the y axis not truncated, but the x axis still has a problem. Can some one tell me how to fix this issue.

Hi @jacksonan1! If you could please make a new topic for this question, and format a more complete reproducible example, that'll help us help you

I suspect the issue will be addressed by changing the settings in `scale_x_continuous`.

From the documentation,

`limits`: A numeric vector of length two providing limits of the scale. Use `NA` to refer to the existing minimum or maximum.

`expand`: Vector of range expansion constants used to add some padding around the data, to ensure that they are placed some distance away from the axes. Use the convenience function `expand_scale()` to generate the values for the `expand` argument. The defaults are to expand the scale by 5% on each side for continuous variables, and by 0.6 units on each side for discrete variables.|

So you might first just remove the limits and expand in scale_x_continuous and see if that delivers the plot you were hoping for. You can also add padding by increasing the values in `expand`.

For additional help, a reprex is really the way to go.

Since this is off-topic from Read.table with no header - #21, where it was originally posted, I took the liberty of converting your reply-post into a new topic thread, as rensa suggested

I used reprex for my post so I am unsure why there was a problem.

I will repost again with reprex.

If you use reprex, the generated images will be on your clipboard in addition to the formatted code (input and output).

I used reprex and got this error:
Error: .onLoad failed in loadNamespace() for 'callr', details: call: loadNamespace(name) error: there is no package called ‘debugme’

Can you tell me what I did wrong?

It looks like you might not have the dependencies installed. Try reinstalling reprex by running

``````install.packages("reprex", dependencies = TRUE)
``````

I ran the command:
#Install reprex package
install.packages("reprex", dependencies = TRUE)
library(reprex)

It loaded okay. I then hit command C to copy. Then I ran reprex() and got this error.
Rendering reprex... Error: .onLoad failed in loadNamespace() for 'callr', details: call: loadNamespace(name) error: there is no package called ‘debugme’

I stopped at this point. Do you have any idea why the reprex() command gives me the error?

Could you retry installing the package, and explicitly install `callr` ahead of that, just in case;

``````install.packages("callr")
install.packages("reprex", dependencies = TRUE)
``````

and then retry?

This is what I did:

1. install.packages("callr")
install.packages("reprex", dependencies = TRUE)
library(reprex)
2.Copied the code to clip board -ctrl copy
3.used command reprex()
I got this error message:

Rendering reprex... Error: .onLoad failed in loadNamespace() for 'callr', details: call: loadNamespace(name) error: there is no package called ‘debugme’

Did I enter an incorrect command or are my methods faulty? I appreciate your patience.

`reprex()` is failing because of a second-degree missing dependency: `reprex` depends on `callr`, but `callr` depends on `debugme` — and you’re missing `debugme`. Try:

``````install.packages("callr", dependencies = TRUE)
``````

Hopefully that will clear this up! By the way, if you’re using RStudio I highly recommend trying the Addin that comes along when you install `reprex` — all you have to do is select the code you want to run through `reprex()` and choose “Reprex selection” from the Addins menu. Super quick! (If you’ve never heard of Addins before and don’t know where the Addins menu is, see here: https://rstudio.github.io/rstudioaddins/#running-addins)

1 Like

I tried the command :

`install.packages("callr", dependencies = TRUE)` and it installed with no error.

I then copied code to the clipboard and used the command:

Reprex() and got this error which is different from the other error.

``````Rendering reprex...

Error in loadNamespace(name) : there is no package called ‘callr’
``````

When I tried to install the addins I got the following error and there was no addin on my tool bar.

``````ERROR: dependency 'shiny' is not available for package 'addinexamples'

Error: Command failed (1)

2: In utils::install.packages(pkgs, repos = repos, type = type, dependencies = dependencies,  :

installation of package ‘shiny’ had non-zero exit status
``````

It looks like something did go wrong in the `callr` install, because `reprex` can't find `callr` when it goes looking for it. I can't tell what the problem might have been without seeing the console output that was generated when you ran the install command. But it looks like something also went wrong when R was trying to install the `shiny` package, which is in the dependency chain for these packages.

However, I also notice that you seem to be running a pretty old version of R (3.3 — the current version is 3.5, and R versions are released annually). Running older R will make it increasingly difficult to install packages, since pre-compiled binaries for up-to-date packages won't be available.

Before you chase these package problems any further, I'd consider upgrading R to the current version. Warning: this will require you to reinstall your packages, but the process should (hopefully!) go more smoothly. If you go this route, I'd start by installing `tidyverse`, which will give you `reprex` (and everything it needs to run) along with `ggplot2`, `dplyr`, and the rest. FYI, within RStudio, there's a nice interface for installing packages — click the Install button on the Packages pane.

1 Like

This is what I got after updating R and R studio. I had no idea that having the older versions of both was a problem for reprex.

Thanks for pointing it out since I had no clue.

Is the code below now suitable for posting? If I had a data frame should it be posted in the same manner?

``` r

#CODE THAT RUN GRAPH WITH NO LEGEND AND FAR X GOes TO X AXIS

ggplot() + geom_density(aes(x=sim), colour="red", linetype="longdash",data=data) +

geom_density(aes(x=dv,col=sim), colour="blue", linetype="solid",data=data) +

scale_y_continuous(limits = c(0,NA),expand=c(0,0)) +

scale_x_continuous(limits = c(0,13),expand=c(0,0)) +

xlab("Concerta Peak1 Cmax Distribution") +

ylab("Density")

#> Error in ggplot(): could not find function "ggplot"

```

Created on 2018-10-01 by the reprex package (v0.2.1)

The error you're getting above is because the reprex needs to be self-contained that means that you need to have `library(ggplot2)` inside of the reprex, otherwise you'll get the error you did, since it doesn't have the function loaded (because ggplot2 isn't loaded).

From Jenny's reprex slide deck here:

1 Like

Ok. I had used the library (ggplot2) to prepare the initial plot so I thought that would suffice.

I will include it as part of the reprex code.

Yeah, reprex doesn't know about anything you run outside of the reprex (that's how it's self-contained, which means others can run your code to reproduce your issue)

I shall endeavor to become a more competent reprex user although I may make an error from time to time.

Thanks for your help and patience.

1 Like

No worries! Honestly, getting better at making reproducible examples is a great way to improve your coding in general — I can't tell you how many times I've solved my own problem en route to making a reproducible example.

Thanks for sticking with this! I really do think it's worth it, once you wrap your head around it. I highly recommend watching the recent webinar (where that slide @mara posted came from), which might help to give you a bit of the bigger picture:

I know this can feel like a lot of hoops to jump through when you're just getting started, but learning to reason about whether a chunk of code is self-contained or not is a really important step in leveling up your programming practice. And the `reprex` package is a fantastic tool for sharing code with other people to get their help. Once you get used to doing things the `reprex` way, it cuts down enormously on the time you spend going back and forth trying to explain what your problem is, and it makes your questions much more fun and appealing for other people to answer, which over time gets you better answers faster.