How to plot two data series on bar chart with separate range axes - jfreechart

I was wondering if it is possible to plot (XYPlot) a dataset in JFreeChart where the series contained in the dataset are plotted against separate y-axis ranges. The date is clustered by category/timestamp, e.g. if my dataset was this:
Timestamp Val1 Val2
2019-04-26 0.6 603
2019-04-25 2.1 1040
2019-04-24 4.1 255
It is impractical to plot both value series on the same range axis.
I've attempted extracting each series into its own dataset, so that I can call plot.mapDataSetToRangeAxis(); but when I add multiple datasets to the plot, the bars tend to render on top of each other. Perhaps I'm missing something simple?
There are a few posts that address separate elements of what I'm looking for, but think I need something that combines these two:
JFreeChart - XYBarChart Show Separate Bars for Each Series
Setting different y-axis for two series with JFreeChart
Here is the python code I'm currently using—inside inductive automation/ignition's reporting module; they allow you configure the JFreeChart prior to rendering.
def configureChart(chart):
from org.jfree.chart.axis import DateAxis
from org.jfree.data.xy import XYSeries, XYSeriesCollection
from org.jfree.chart.renderer.xy import ClusteredXYBarRenderer
from java.awt import Color
from java.text import NumberFormat
class mins_to_str(NumberFormat):
def format(self,*args,**kwargs):
r = ''
number = args[0]
hrs = number//60
mins = number%60
r = '%02i:%02i' %(hrs,mins)
if len(args)>1:
toAppendTo = args[1]
pos = args[2].getField()
r = toAppendTo.insert(pos,r)
return r
plt = chart.getPlot()
renderer = ClusteredXYBarRenderer
xax = DateAxis()
plt.setDomainAxis(xax)
for i in range(plt.getDatasetCount()):
d = plt.getDataset(i)
dsc = XYSeriesCollection()
series = XYSeries(d.getSeriesKey(0))
print('SERIES [%s]' %series)
for r in range(d.getItemCount(0)):
xv = d.getXValue(0,r)
yv = d.getYValue(0,r)
print(' X: %s (%s)' %(xv,type(xv)))
print(' Y: %s (%s)' %(yv,type(yv)))
series.add(xv,yv)
dsc.addSeries(series)
plt.setDataset(i,dsc) # assuming all of my series need to be in the same dsc for this to work...
plt.setRenderer(i,renderer)
if i > 0:
plt.mapDatasetToRangeAxis(i,1)
else:
plt.mapDatasetToRangeAxis(i,0)
plt.getRangeAxis(0).setNumberFormatOverride(mins_to_str())
Currently, I'm getting this:
Any ideas/help would be greatly appreciated.

Here is a time series chart within ignition's Report developing environment.
This one is completely setup using the Chart Options and just a tiny bit of JFreeChart scripting. Should be similar to designing a bar chart. Hope it helps.
JFreeChart Script -
#Import Java classes - Color & JFreeChart
from java.awt import Color
from org.jfree.chart.plot import CategoryPlot
#get Plot of Current Chart
plot = chart.getPlot()
#Set color of domain and range gridlines
plot.setDomainGridlinePaint(Color.black)
plot.setRangeGridlinePaint(Color.black)

Related

How to use extra extra_x_ranges, extra_y_ranges with add_tile in bokeh

I want to use long/lat (EPSG:4326) coordinates in a bokeh plot and have a map in the Background.
I tried with the tile provider maps as suggested in bokeh: Mapping geo data.
But the format is in web mercator coordinates (EPSG:3857) and I don't want to convert my coordinates.
The general question how to do this is unanswered in Is it possible to set figure axis_type in bokeh to geographical (long/lat)?
My idea was to use extra axes:
from bokeh.plotting import figure, show
from bokeh.models import Range1d, LinearAxis
from bokeh.tile_providers import CARTODBPOSITRON, get_provider
tile_provider = get_provider(CARTODBPOSITRON)
p = figure(x_range=(-180, 180), y_range=(-90, 90)) # EPSG:4326
# add extra axis
p.extra_x_ranges = {"EPSG:3857x": Range1d(start=-20026376.39, end=20026376.39)}
p.extra_y_ranges = {"EPSG:3857y": Range1d(start=-20048966.10, end=20048966.10)}
# place extra axis
p.add_layout(LinearAxis(x_range_name="EPSG:3857x"), 'above')
p.add_layout(LinearAxis(y_range_name="EPSG:3857y"), 'right')
p.add_tile(tile_provider, x_range_name="EPSG:3857x", y_range_name="EPSG:3857y")
show(p)
But the map is not visible.
Is there a way to use extra axis for a tile_provider?
If you are just asking about displaying lat/lng visually on the axes, then all you have to do is set the axis type to "mercator"
p = figure(x_range=(-2000000, 6000000), y_range=(-1000000, 7000000),
x_axis_type="mercator", y_axis_type="mercator")
This is demonstrated on the documentation page you linked.
If you are asking about using data that is in lan/lng coordinates to plot on a tile plot, then you will need to convert it to Web Mercator first. The underlying coordinate system for tiles is always Web Mercator.
If you are asking about something else, then your question is not clear (please update to clarify).

Is there an R function that allows me to combine 2 graphs?

#I'm trying to do a taste wheel, like the Meilgaard wheel.
I'm not trying to do anything complex I only want to plot together a pie chart and a PCA plot.
I would like to have the piechart bigger than the PCA graph.
I would like to plot the pie graph with both the two PCA graphs or with the fviz_pca_biplot.
The following is the PCA:
principal4<-PCA(ExpertWine2,scale.unit = T,ind.sup = NULL,quanti.sup =29:30,quali.sup =1,graph = T,axes = c(1,2),ncp = 3)
fviz_pca_biplot(principal4)
The following is the pie chart:
slices <- c(10, 10, 10, 10)
lbls <- c("acidity", "freshness", "sweetness", "corposity")
coloris = c("gold", "cyan", "indianred1", "burlywood4")
cake=pie(slices, labels = lbls, init.angle=45, main="Pie Chart of tastes",col=coloris)
There are multiple packages aimed at solving this. It will depend on whether you as using a base r plotting implementation or something like ggplot.
I would reccomend looking at bentobox (strict positioning), patchwork (auto positioning) and cowplot (somewhere in the middle). Patchwork is primarily aimed at ggplot but the other two should work with any plotting method.
bento box:
https://github.com/PhanstielLab/BentoBox
patchwork:
https://patchwork.data-imaginist.com/
cowplot:
https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html

Plotly in R Power BI

How I can create interactive R plots in Power BI (for example Plotly)? Below code doesn't return any error, but also doesn't show chart:
library(plotly)
library(ggplot2)
z = ggplot(data = dataset) + geom_point(mapping = aes(x = Console, y = Search))
ggplotly(z)
Data source:
source <- "https://cdn.rawgit.com/BlueGranite/Microsoft-R-Resources/master/power-bi/gameconsole.csv"
game.console <- read.csv(source, header = TRUE)
According to this question in Power BI's Community forums
Plotly lib is supported as part of HTML support for R powered Custom
Visuals only, not R Visuals in general currently.
Plotly can only be used if it produces an IMAGE\PNG for R visuals in
PBI. Not HTML.
For Custom Visuals we have an upcoming feature which will also enable R-based custom visuals to render as htmls.
Hope this helps.
The reason is that right now Power BI only supports render charts created by R visualization component as PNG.
Try the following:
p <- plot_ly(x = dataset$period, y = dataset$mean, name = "spline", line = list(shape = "spline"))
plotly_IMAGE(p, format = "png", out_file = "out.png")
But the problem with this is that, though rendered by plotly, the visualizations will not be interactive since its just a PNG image.
If you want to create interactive visualizations using plotly. The only way you can do is to create a custom Power BI visualization and import it to your report. See this post for a good introduction.
PowerBI only supports charts rendered as PNG while plotly format is in HTML. You can try to save the chart as PNG then print it in the R console inside PowerBI.
You first have to register a plotly account here.
After registration, on the top right corner arrow next to your account name and click on Settings -> API keys. You will be able to generate API key. Copy and paste your username and API key using this code.
Sys.setenv("plotly_username"="....")
Sys.setenv("plotly_api_key"=".....")
Then add this code in to turn the plot into png format and print it out.
fig <- plot_ly(x = dataset$Console, y = dataset$Search)
Png <- plotly_IMAGE(fig, out_file = "plotly-test-image.png")
print(Png)
As mentioned in another answer, this plot won't be interactive as plot in PowerBI. To create an interactive plot in PowerBI, you have to create a custom visual. Follow an R custom visual example here or radacad example here.

Create Fatter Candlesticks in JFreeChart

I'm developing an app that displays daily financial data, and have chosen to use JFreeChart. I was able to learn how to create a candlestick chart, but my problem lies in customization.
You see, what I'm aiming for is more along the lines of
While, so far all I've been able to manage is
.
No matter how far I zoom in, the candlesticks do not increase in width.
I'm fairly certain that somehow the thin candlesticks have something to do with being bound to a certain time range.. I've tried to remedy that but am not sure what I'm doing wrong here.
SSCE
public void showStockHistory(OHLCDataset dataset, String stockName) {
JFreeChart candleChart = ChartFactory.createCandlestickChart("History of " + stockName, "Date", "Stock Points", dataset, true);
XYPlot plot = candleChart.getXYPlot();
plot.setDomainPannable(true);
plot.setRangePannable(true);
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
NumberAxis range = (NumberAxis)plot.getRangeAxis();
range.setUpperMargin(0.0D);
range.setLowerMargin(0.0D);
range.setAutoRange(true);
range.setAutoRangeIncludesZero(false);
ChartPanel chartPanel = new ChartPanel(candleChart);
chartPanel.setMouseWheelEnabled(true);
chartPanel.setMouseZoomable(true);
getViewport().add(chartPanel);
}
Although my given example seems to have to no differing method calls from the demo in the first picture's code above, it nevertheless only shows thin candlesticks. I assume this to be some kind of bug.
However, I was able to rectify the issue as follows:
getting the renderer for the chart,
casting it to a type of CandlestickRenderer, and
setting its setAutoWidthMethod() method to CandlestickRenderer.WIDTHMETHOD_SMALLEST.
This is how you do it:
JFreeChart candleChart = ChartFactory.createCandlestickChart(
"History of " + stockName, "Date", "Stock Points", dataset, true);
XYPlot plot = candleChart.getXYPlot();
CandlestickRenderer renderer = (CandlestickRenderer) plot.getRenderer();
renderer.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST);

XYSeries autorange loss of precision?

I'm creating an XYSeries in JFreeChart 1.0.17 as shown below, and building a chart out of it dynamically. The x, y data that are added can be of any magnitude, but when the x and y values are very small fractions -- 1e-5 or less -- the autorange calculations seem not to work, and the plot produced will be just a flat line. In this case multiplying all the data by a factor of 10,000 before adding them to the series leads to a correct plot. The data are added as doubles, and I've looked at the XYSeries object in a debugger to make sure the correct values are ending up there. Am I missing something? Is there some way to help the autorange calculations do the right thing?
series = new XYSeries("Heartbeat");
final XYSeriesCollection data = new XYSeriesCollection(series);
chart = ChartFactory.createXYLineChart("", "", "", data, PlotOrientation.VERTICAL, false, true, false);
final XYPlot plot = chart.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
((NumberAxis) domain).setAutoRangeIncludesZero(false);
ValueAxis range = plot.getRangeAxis();
range.setAutoRange(true);
((NumberAxis) range).setAutoRangeIncludesZero(false);
The ValueAxis class has an autoRangeMinimumSize attribute which defaults to 0.00000001. When the range is automatically calculated, the axis won't let it go below this size. It is intended for the case where all your data values are the same, in which case it determines the axis length. But as you've found, it causes problems if all your data values are less than 0.00000001, so you should set it to something lower instead of using the default.

Resources