Error in diagram positioning

,

i printed a pdf file with RMD. i plotted a line graph but it is right aligned when on the pdf as shown below

This is the code i used to in plotting the graph
ggplot(transaction_by_date, aes(x = DATE, y = transaction_count)) +
geom_line() +
labs(x = "Date", y = "Number of transactions", title = "Transactions over time") +
scale_x_date(breaks = "1 month") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

and the fig.align is set to center. How do i fix this ?

its really surrounding markdown code that would determine the placement of the thing; rather than the code for the thing being placed itself. i.e. your ggplot code doesnt tell us much aside from the content of the plot.

Visually it looks like you have text 'Setting plot themes to format graphs' to the left and the plot is making space for it....

i used this code
theme_set(theme_bw())
theme_update(plot.title = element_text(hjust = 0.5))
before ploting the graph

... thats only more styling for the graph.

What makes the text appear ?

knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(linewidth=80)

Lets take another tack...

---
title: "Retail Strategy and Analytics "
author: "Ogai"
date: "2023-04-12"
output: pdf_document
pdf_document:
df_print: default
highlight: tango
keep_tex: yes
latex_engine: xelatex
header-includes: \usepackage{fvextra}\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}
---

```{r setup, include=FALSE}
# set options for R markdown knitting
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(linewidth=80)
# set up line wrapping in MD knit output
library(knitr)
hook_output = knit_hooks$get("output")
knit_hooks$set(output = function(x, options)
{
 # this hook is used only when the linewidth option is not NULL
 if (!is.null(n <- options$linewidth))
 {
 x = knitr:::split_lines(x)
 # any lines wider than n should be wrapped
 if (any(nchar(x) > n))
 x = strwrap(x, width = n)
 x = paste(x, collapse = "\n")
 }
 hook_output(x, options)
})

Is this enough ?

When I run your code , I dont see any problem to fix.
When I extend your code to add things that could possible break or be wrong; I still dont.
What does this result in for you ?

---
title: "Retail Strategy and Analytics "
author: "Ogai"
date: "2023-04-12"
output: pdf_document
pdf_document:
  df_print: default
highlight: tango
keep_tex: yes
latex_engine: xelatex
header-includes: \usepackage{fvextra}\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}
---
  
```{r setup, include=FALSE}
# set options for R markdown knitting
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(linewidth=80)
```


```{r knitr line wrap setup, include=FALSE}
# set up line wrapping in MD knit output
library(knitr)
library(tidyverse)
hook_output = knit_hooks$get("output")
knit_hooks$set(output = function(x, options)
{
  # this hook is used only when the linewidth option is not NULL
  if (!is.null(n <- options$linewidth))
  {
    x = knitr:::split_lines(x)
    # any lines wider than n should be wrapped
    if (any(nchar(x) > n))
      x = strwrap(x, width = n)
    x = paste(x, collapse = "\n")
  }
  hook_output(x, options)
})
```

Some Plain Text
```{r, include=TRUE}
ggplot(data=iris,aes(x=Sepal.Length,y=Petal.Length,color=Species))+geom_point()
```
Some More Plain Text

I used your code but it didn't solve the problem. The problem i have is that the graph i plotted is not center aligned and from what i have leant so far from you , the problem is not from the ggplot code. Ironically I made over 3 plots but it is just that one plot that isn't center aligned. Is there any other technique that can be used to fix this

I cant fix what I can't get my hands on to investigate and try things with.
If you can not provide a reproducible example that can be fixed, I wont be able to fix it. Thats just one of those things ... sorry.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.