0

And I apologize in advance for the silly question. Just teach ... I am trying to parse the material from the tutorial.

https://ru.it-brain.online/tutorial/ggplot2/ggplot2_working_with_axes/

In particular, I'm trying to highlight a segment on the chart. But for some reason, it is not the area I line that is displayed.

Here is my code.


    ---
    title: "Pres"
    output:
      html_document:
        #code_folding: hide
        css: Проба.css
        df_print: paged
        toc_float: yes
      pdf_document:
        latex_engine: xelatex
      word_document: default
    mainfont: Times New Roman
    ---
    
    ```{r}
    library("readr")
    library("data.table")
    library("here")
    library("DT")
    library("ggplot2")
    ```
    
    ```{r}
    this_dir <- here()
    setwd(this_dir)
    
    ```
    
    ```{r}
    data_file_name<-"Проба.csv"
    ```
    
    ```{r}
    data_file <- fread(data_file_name, showProgress = TRUE, sep = ";", quote = "", header = TRUE, stringsAsFactors=TRUE)
    ```
    
    # {.tabset }
    
    ## Ttab 1
    ```{r}
    datatable(data_file,
              rownames = FALSE,
              options = list(
                columnDefs = list(list(className = 'dt-center', targets = 0:4))
                )
              )
    ```
    
    ## Ttab 2
    ```{r}
    pts <- function(data, xcol, ycol, ccol, name_ccol, title) {
      x_var <- enquo(xcol)
      y_var <- enquo(ycol)
      c_col <- enquo(ccol)
      #name_c_col<- enquo(name_ccol)
      c_title<- enquo(title)
      ggplot(data, aes(x = !!x_var, y = !!y_var, colour = !!c_col)) +
        geom_point(
          asp = 1,
         pch = 0, 
         cex = 4
        ) +
        annotate("segment", x = 5, xend = 20, y = 5, yend = 20, colour = "purple", alpha=1, size=1) + 
        labs(y="Petal length (cm)", x = "Sepal length (cm)", colour = name_ccol) +
        ggtitle(c_title) +
        theme(legend.position="bottom",
              legend.title = element_text(colour = "blue", size = 10, face = "bold"),
              legend.text = element_text(colour = "#00FF00", size = 8, face = "bold"))
     
    }
    pts(data_file, `№ кв.`, `№ подъезда`, `Возраст`, "№\nпадика",`Название диаграммы`)
    ```
    ###
    
    <p style="font-family: times, serif; font-size:14pt; font-style:italic">
        Здесь текст
    </p>

Please tell me what I'm doing wrong.

Here are all the files I used, in particular: - the code itself, - the css file, - the data file.

https://yadi.sk/d/OoRBkkinnJ0xQQ

This is my result enter image description here

This is what I was trying to achieve enter image description here

It doesn't matter to me which area will be selected. For this reason, any.

  • 2
    Could you be more specific on what you are trying to achieve? Which segment do you want to highlight? And. To help us to help you could you please make your issue reproducible by sharing a sample of your data. See [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – stefan Nov 08 '20 at 07:01
  • I tried to change the information, added material. Now everything, it seems to me, is available. I am ready to answer additional questions. –  Nov 08 '20 at 22:15
  • As you want a rectangle try with `annotate("rect", xmin = 5, xmax = 20, ymin = 5, ymax = 20, fill = "purple", alpha=.5, size=1)`. For geom_rect you have to use xmin, xmax, ... instead of x, xend, ... . Also. Use fill instead of color. And to get some transparency use e.g. alpha = .5. BTW: For future questions I would suggest to use the latin alphabet as cyrillic makes it hard to run your code. – stefan Nov 09 '20 at 07:57
  • Thank you so much. This is exactly what you need. Can you please tell me where I could read about this? In the documentation for the function, only the "geom" parameter is described, and there is very little information and the types of geopometries are also not indicated. Thanks. –  Nov 09 '20 at 10:04

0 Answers0