Aim: plot a column chart representing concentration values at discrete sites
Problem: the 14 site labels are numeric, so I think ggplot2 is assuming continuous data and adding spaces for what it sees as 'missing numbers'. I only want 14 columns with 14 marks/labels, relative to the 14 values in the dataframe. I've tried assigning the sites as factors and characters but neither work.
Also, how do you ensure the y-axis ends at '0', so the bottom of the columns meet the x-axis?
Thanks
Data:
Sites: 2,4,6,7,8,9,10,11,12,13,14,15,16,17
Concentration: 10,16,3,15,17,10,11,19,14,12,14,13,18,16
You have two questions in one with two pretty straightforward answers:
1. How to force a discrete axis when your column is a continuous one? To make ggplot2 draw a discrete axis, the data must be discrete. You can force your numeric data to be discrete by converting to a factor. So, instead of x=Sites in your plot code, use x=as.factor(Sites).
2. How to eliminate the white space below the columns in a column plot? You can control the limits of the y axis via the scale_y_continuous() function. By default, the limits extend a bit past the actual data (in this case, from 0 to the max Concentration). You can override that behavior via the expand= argument. Check the documentation for expansion() for more details, but here I'm going to use mult=, which uses a multiplication to find the new limits based on the data. I'm using 0 for the lower limit to make the lower axis limit equal the minimum in your data (0), and 0.05 as the upper limit to expand the chart limits about 5% past the max value (this is default, I believe).
Here's the code and resulting plot.
library(ggplot2)
df <- data.frame(
Sites = c(2,4,6,7,8,9,10,11,12,13,14,15,16,17),
Concentration = c(10,16,3,15,17,10,11,19,14,12,14,13,18,16)
)
ggplot(df, aes(x=as.factor(Sites), y=Concentration)) +
geom_col(color="black", fill="lightblue") +
scale_y_continuous(expand=expansion(mult=c(0, 0.05))) +
theme_bw()
Say you have the matrix given by three arrays, being:
x = N-dimensional array.
y = M-dimensional array.
And z is a set of "somewhat random" values from -0.3 to 0.3 in a NxM shape. I need to create a plot in which the x values are in the x-axis, y values are in the y-axis and using z as the source to indicate the intensity of each pixel with a color map.
So far, I have tried using
plt.contourf(x,y,z)
and the resulting plot is very nice for me (attached at the end of this paragraph), but a smoothing is automatically applied to the plot! I need to be able to distinguish the pixels and I cannot find a way to do it.
contourf result
I have also studied the possibility of using
ax.matshow(z)
in order to sucesfully see the pixels... but then I am struggling trying to personalize the x and y axis, since only the index of the pixel is shown (see below).
matshow result
Would you please give me some ideas? Thank you.
Without more information on your x,y data it's hard to know, but I would guess you are looking for pcolormesh.
plt.pcolormesh(x,y,z)
This would take the x and y data as input and hence shows the z data at the appropriate coordinates.
You can use imshow with the keyword interpolation='nearest'.
plt.imshow(z, interpolation='nearest')
i am using MPAndroid charting toolkit for data visualization. chart plotting smooth in MPAndroid but problem arise when i try plot dual y axis(left& right). As right axis appear data points on spread over the x axis completely. all data points appearing on the left of chart. How can i spread the data points ?
mChart = (LineChart) findViewById(R.id.Chart);
mChart.setGridBackgroundColor(Color.parseColor("#F4F4F4"));
mChart.setDrawGridBackground(false);
mChart.setTouchEnabled(true);
mChart.setHighlightEnabled(false);
mChart.setDragEnabled(false);
mChart.setScaleEnabled(true);
mChart.setPinchZoom(true);
mChart.setDescription("");
mChart.getAxisLeft().setAxisMaxValue(YMaxValue);
mChart.getAxisLeft().setAxisMinValue(YMinValue);
mChart.getAxisLeft().setStartAtZero(false);
mChart.getAxisRight().setEnabled(false);
if(General.InnerClass.Y2AxisValues.size()>0)
{
mChart.getAxisRight().setEnabled(true);
mChart.getAxisRight().setSpaceBottom(12.25f);
mChart.getAxisRight().setAxisMaxValue(Y2MaxValue);
mChart.getAxisRight().setAxisMinValue(Y2MinValue);
mChart.getAxisRight().setStartAtZero(false);
mChart.getXAxis().setAvoidFirstLastClipping(true);
}
mChart.setData(data);
progbar.setVisibility(View.GONE);
mChart.invalidate();
Before
After
The data points are exactly where they should be. The only thing that has changed is the range of values that is displayed on each axis.
--> this range is now significationly lower because only one set of data is represented by each axis, before, one axis had to scale large enough to display both datasets.
I suggest you read the documentation of the YAxis and simply increase the range of values that should be displayed on the axis.
while plotting y2 axis providing the x values is the problem. values for the x axis should be provide for single time. providing twice add the values to x axis exiting values get doubled.
thnx #philipp
The application I'm currently working on will provide the ability to take unknown values based on known values from a line graph. Here's the problem. The line graph will not exist theoretically. But the information provided and past data will be used to be able to find that unknown.
For example
x axis: known
y axis: unknown
plotted line graph: partially known
So in actuality we can draw a graph with the x, y axis and plot the line graph with what we actually know. So say our knowns are:
x axis: 10; y axis: 15; plot: 160 line
x axis: 12.7; y axis: 18; plot: 160 line
x axis: 15; y axis: 22; plot: 160 line
so if we drew this, anything between 10-15 we could detect the y axis by straight line up to the 160 line and then accross to the y axis to find the answer. Thus, more inputted information would eventually lead to every possibility between 1-100 to be answerable.
However, my issue is that in the sense of programatical logic I can't think of where to start. For instance, I'll have my known values such as;
x = 10; plot = 160; therefore y = 15
And could be said for the other examples, though how can I manipulate them numbers to find unknowns which I would find on the graph with a simple ruler?
So programatically it would look like
x = 13; plot = 160; therefore y = ?;
If anyone has much experience with line graphs or at least with the formulae to unknowns it would be great if you could provide me with some starting information or point me in the right direction. As of right now, I have no idea where to start.
edit;
I'm coming to the conclusion that I will have to interpolate between the two y known values for a ratio of x difference?
so for instance, an input of 13 would show as
input: 13; find closest less-than to that; find closest greater-than to that;
returns 12.7 and 15
now find a ratio difference between them
take their known y values
lowest: 18; highest: 22
now lerp(double lowest, double highest, double ratio of difference between xs
Do I appear to be taking the right path?
Polynomial Interpolation
y = y0+(y1+y0)*((x-x0)/(x1+x0));
I'm using coreplot for my project,the problem i'm struggling for past few hours is,i need to update the y axis values every time user enter changes value.To constraint the y axis range i'm using [graph.defaultPlotSpace scaleToFitPlots:[graph allPlots]]but with this i'm facing issue with x axis position,it's changing every time i change the value for y axis.
As you can see,the x axis is moving from down to up.How can i fix this problem?
-scaleToFitPlots: adjusts the plot space to fit the plot data exactly. You can use it as a starting point to fit the data and then expand the resulting range to leave extra space around the edges or to fix the location of one of the ranges. For example, you could adjust the calculated yRange so the location is always at zero (0).