0
votes

When I go line by line through my code, I get the following plot:

line by line plot

But then when I render rmarkdown to HTML report, the plot looks like this:

rmarkdown html plot

I am unable to give reproducible code to create the plots as the code is property of my company. But they are created using three functions, the first adds the circle points, the second adds a layer for the the star points, and the final adds the black line.

Somehow, when rmarkdown is rendering the report, the first layer of the plot is condensed to the bottom.

The chunk options are set to:

{r, echo = FALSE, message = FALSE, results = "asis"}

1
If you start a clean R session and then run only the code in the Rmarkdown report interactively, what happens? - eipi10
The second plot looks like the kind of thing that happens when one or more continuous variables get converted to a factor. It looks like that might have happened to the data represented by the pink and purple circles. But it will be difficult to provide a firm diagnosis and solution without seeing code and data. - eipi10
I'm not sure why you're getting different results interactively and with compilation. Is the "interactive" code in a separate R script or are you running the actual code in the rmarkdown document interactively? To run the code in the rmarkdown document interactively (assuming you're in RStudio), put the cursor just below the point at which the plot is generated in the rmarkdown document, click on the Run button and select Run all chunks above. - eipi10
format outputs a character string, so your numeric data was converted to character. character and factor classes are different, but they're both categorical and behave essentially the same way for the purposes of this issue. - eipi10
We ask for a reproducible example exactly to avoid this type of confusion about what code actually produced the output in a question. - eipi10

1 Answers

0
votes

Following up on my comment, I think it's likely that somehow the data in the circles data frame is being converted to factor class. I don't know why it's happening with rmarkdown compilation and not interactively (assuming you're running the rmarkdown code chunks interactively, rather than a separate script containing what is believed to be the same code). In the absence of additional inspiration, we'll need to see code and data to answer that question. For now, here's an example that produces output analogous to your problem graph:

Set up two data frames using the built-in mtcars data:

data(mtcars)
dat1 = mtcars[1:10,]
dat2 = mtcars[11:20, ]

# Convert the `mpg` and `wt` columns to factor in `dat1` only:
dat1$mpg = factor(dat1$mpg)
dat1$wt = factor(dat1$wt)

# Increase the values of dat2$wt so they'll plot to the right of the values in dat1$wt
dat2$wt = dat2$wt * 10

Now for the plot: The plot looks the way it does because the values in dat1 are treated as categorical. So the axis labels are equal to the values of the points, but the locations (the x-y coordinates) where they are plotted are equal to the integers 1 through n where n is the number of points.

The continuous values in dat2 are plotted at locations equal to their actual values (i.e., at their "correct" locations) on the same scale, but without any tick marks.

ggplot() +
  geom_point(data=dat1, aes(wt, mpg)) +
  geom_point(data=dat2, aes(wt, mpg)) +
  theme_classic()

enter image description here