R: interactive plots (tooltips): rCharts dimple plot: formatting axis
Here is one way to solve (1) and (2). The argument showPercent
is not to add % to the values, but to recompute the values so that they stack up to 100% which is why you are seeing the behavior you pointed out.
At this point, you will see that we are still having to write custom javascript to tweak the x-axis to get it to display the way we want it to. In future iterations, we will strive to allow the entire dimple API to be accessible within rCharts.
df <- read.csv("ps-income-shares.csv")
p <- dPlot(
value ~ Year,
groups = c("Fractile"),
data = df,
type = "line",
bounds = list(x = 50, y = 50, height = 300, width = 500)
)
p$xAxis(inputFormat = '%Y-%m-%d', outputFormat = '%Y')
p$yAxis(outputFormat = "%")
p$setTemplate(afterScript = "
<script>
myChart.axes[0].timeField = 'Year'
myChart.axes[0].timePeriod = d3.time.years
myChart.axes[0].timeInterval = 5
myChart.draw()
//if we wanted to change our line width to match the ggplot chart
myChart.series[0].shapes.style('stroke-width',1);
</script>
")
p
How to change the axis title with rCharts, dPlot and dimple
Sorry I just saw this. Thanks John for answering.
With rCharts, we can take advantage of the afterScript template to add this. If there is only one chart in the DOM, we can use John's example unmodified.
d1$setTemplate(
afterScript =
'
d3.selectAll(".axis.title")
.text(function () {
var t = d3.select(this).text();
if (t === "disp") {
return "Displacement";
} else if (t === "mpg") {
return "Miles Per Gallon";
} else {
return t;
}
});
'
)
Please let me know if this you would like an example with multiple charts in the DOM or this does not work for you. Thanks.
Add line to rChart (dimple) scatter plot
Very good question, and I am glad you asked. This functionality will be added with this pull request. For now, you can test it by require(devtools); install_github("rCharts","timelyportfolio",ref="dimple_layer")
. Here is your example reworked to work almost right. Also, I added some other considerations. I'm still trying to get the dimple aggregation working correctly in rCharts. You will see a not so perfect workaround.
library(rCharts)
df <- data.frame(id = 1:10, a = rnorm(10), b = rnorm(10))
d1 <- dPlot(b~a,
groups = "a",
data = df,
type = "bubble"
,defaultColors = "#!['blue']!#"
)
d1$xAxis(type="addMeasureAxis")
d1$yAxis(type="addMeasureAxis",overrideMin = -2, overrideMax = 2)
d1
d1$layer(
y ~ x
,groups = c("x","y")
,data = data.frame(x = c(1,10), y = rep(0,2))
,type="line"
)
d1
For now, a better way to handle might be to use afterScript
to draw a line on top. As you can see this is definitely a work in progress and is far too hacky.
library(rCharts)
df <- data.frame(id = 1:10, a = rnorm(10), b = rnorm(10))
d1 <- dPlot(b~a,
groups = "a",
data = df,
type = "bubble"
,defaultColors = "#!['blue']!#"
)
d1$xAxis(type="addMeasureAxis")
d1$yAxis(type="addMeasureAxis",overrideMin = -2, overrideMax = 2)
d1
d1$setTemplate(
afterScript = sprintf(
'
<script>
var line = d3.svg.line()
.x(function(d) { return myChart.axes[0]._draw.scale()(d.x); })
.y(function(d) { return myChart.axes[1]._draw.scale()(d.y); });
d3.select("#%s svg g")
.append("path")
.datum([{x:myChart.axes[0]._min,y:0},{x:myChart.axes[0]._max,y:0}])
.attr("d",line)
.style("stroke","blue")
.style("stroke-width",3)
</script>
'
,d1$params$dom
))
d1
rcharts dimple bubble chart in shiny
Hi you have to put width="100%"
in dplot()
, like this :
d1 <- dPlot(
x = c( "Channel", "PriceTier"),
y = "Owner",
z = "Distribution",
groups = "PriceTier",
data = data,
type = "bubble",
aggregate = "dimple.aggregateMethod.max",
width="100%"
)
rcharts: nPlot Formating x-axis with dates
In R as.numeric(someDate) is days since origin. Here we need milliseconds from origin.
So it should be
df <- data.frame(date=seq.Date(from=as.Date("2012/01/01", origin="1970-01-01"), to=as.Date("2012/02/28", origin="1970-01-01"), by="1 day"), y=1:59)
n <- nPlot(y ~ date, group = "team", data = df, type = "lineChart")
n$xAxis(
tickFormat = "#!
function(d) {return d3.time.format('%Y-%m-%d')(new Date(d*1000*3600*24));}
!#",
rotateLabels = -90
)
n
Width/height of the plot area in a dimple.chart
Try chart._widthPixels()
and chart._heightPixels()
they aren't part of the public API but are used extensively internally for the plot size.
Related Topics
Paste Several Column Values into One Value in R
Visualising and Rotating a Matrix
Programming with Ggplot2 and Dplyr
Control Number Formatting in Shiny's Implementation of Datatable
Plotting Pie Charts in Ggplot2
How to Reference Column Names That Start with a Number, in Data.Table
Import All the Functions of a Package Except One When Building a Package
Rotate X Axis Labels 45 Degrees on Grouped Bar Plot R
Rscript Detect If R Script Is Being Called/Sourced from Another Script
Select Rows in a Dataframe in R Based on Values in One Row
Print the Sourced R File to an Appendix Using Sweave
R Data.Table Join: SQL "Select *" Alike Syntax in Joined Tables
Syntax Highlighting for Python Chunks Does Not Work
Using Override.Aes() in Ggplot2 with Layered Symbols (R)