问题
when I run the r code in the console, I get the output I expected, but when I run it in rmarkdown I get this result:
The complete rmarkdown minimum example looks like this:
---
title: "Untitled"
author: "sda"
output: html_document
---
```{r}
library(dplyr)
library(tidyr)
library(rCharts)
library(data.table)
```
```{r results = 'asis', comment = F, message = F, tidy = F, echo=F, cache=F}
myData <- data.table(XAxis = rep(seq(0,length.out = 3),each = 2),
Value = c(0.6,0.8,0.4,0.55,0.87,0.42),
Type = c("A", "B", "A", "B", "A", "B"))
#' Create a multiBarChart using nvd3 of rCharts
plot <- nPlot(Value ~ XAxis, group = 'Type',
data = myData, type = 'multiBarChart')
plot$chart(forceY = c(0.5, 1))
plot$show('iframesrc', cdn = TRUE)
```
Look the bars start too low
So the forceY does not seem to work correctly. Is there an easy workaround for this?
回答1:
The easiest workaround is to save the plot and display it in an iframe. Your example would look as follows:
---
title: "Untitled"
author: "sda"
output: html_document
---
```{r}
library(dplyr)
library(tidyr)
library(rCharts)
library(data.table)
```
```{r results = 'asis', comment = F, message = F, tidy = F, echo=F, cache=F}
myData <- data.table(XAxis = rep(seq(0,length.out = 3),each = 2),
Value = c(0.6,0.8,0.4,0.55,0.87,0.42),
Type = c("A", "B", "A", "B", "A", "B"))
plot <- nPlot(Value ~ XAxis, group = 'Type',
data = myData, type = 'multiBarChart')
plot$chart(yDomain = c(0.5, 1))
plot$save("plot.html", standalone = TRUE)
```
<iframe src="plot.html" height="500" width="900" frameBorder="0"></iframe>
Note that I used yDomain
instead of forceY
. I did this because forceY
displayed the graph from y=0.4 in my computer. However, the chart will also be shown correctly using forceY
.
You can find the discussion I got the idea for this solution here: https://github.com/ramnathv/rCharts/issues/373
来源:https://stackoverflow.com/questions/29937073/rchart-forcey-multibar-bar-start-not-correct-in-rmarkdown