rmschartofficer

R mschart turn off line smoothing


I'm using mschart version 0.2.6 to draw line charts in R for powerpoint. I get smoothed lines by default but don't want that. How do I turn it off?

I found the following feature request on github but it's closed and I'm not sure how it was resolved: https://github.com/ardata-fr/mschart/issues/25

Here is a working example:

library(officer)
library(mschart)

dat<-data.frame(Year=c(rep("2010",3), rep("2011",3), rep("2012",3)),
            Fruit=c(rep(c("Apples","Oranges","Pears"),3)),
            Count = c(332,229,168,
                      223,189,225,
                      321,378,261), stringsAsFactors=F)

line_chart <- ms_linechart(data = dat, x = "Year",y = "Count", group="Fruit")

doc <- read_pptx()
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- ph_with(x = doc, value = c("Fruit"),
             location = ph_location_type(type = "title") )
doc <- ph_with(doc, line_chart, location = ph_location_type(type="body"))
fileout <- "./Test.pptx" 
print(doc, target = fileout)

Here is what it produces. Notice the Smoothed Line option checked in lower left: enter image description here


Solution

  • Pipe (or wrap) the ms_linechart object into a chart_data_smooth() function with values set to 0.

    library(officer)
    library(mschart)
    library(tidyverse)
    
    line_chart <- ms_linechart(data = dat, x = "Year",y = "Count", group="Fruit") %>% 
                  chart_data_smooth(values = 0) # Here is the code that sets lines to straight.
    
    doc <- read_pptx()
    doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
    doc <- ph_with(x = doc, value = c("Fruit"),
                   location = ph_location_type(type = "title") )
    doc <- ph_with(doc, line_chart, location = ph_location_type(type="body"))
    fileout <- "./Test2.pptx" 
    print(doc, target = fileout)
    

    enter image description here