How do I display all of my x axis labels in my bar plot in ggplot2? - ggplot2

So I have my graph made which is really great. I am now just trying to get each x=axis label to be there (patient column).Is there a way for me to do this in ggplot? (please note patient is just 1-67). I have tried a couple of other methods like lars or +theme(legend.key.size=unit(1,"in"),legend.title=element_text(size=22),legend.text = element_text(size=17))+axis(1,at=midpts,labels=names(Patient)) but neither have worked. Any advice is appreciated!
[![][1]][1]
ggplot(data=V5.ACE2.double.replacement.and.redo.of.AUC.calculation.CSV.file, mapping=aes(x=Patient,y=Fluorescent.sum.over.240.min,fill=Top.20.),las=2)+geom_bar(stat='identity')+theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank(),panel.background=element_blank())+labs(x="\nPatient Sample")+labs(y="\nFluorescence sum over 240 min")+theme(axis.title.x=element_text(size=26))+theme(axis.title.y=element_text(size=26))+theme(axis.text=element_text(size=18))+theme(legend.key.size=unit(1,"in"),legend.title=element_text(size=22),legend.text = element_text(size=17))

Your x-axis is the numeric patient ID and it's getting configured as a continuous scale. It sounds like you want a categorical scale. Turn the Patient column into a factor with something like this:
library(tidyverse)
V5.ACE2.double.replacement.and.redo.of.AUC.calculation.CSV.file <- V5.ACE2.double.replacement.and.redo.of.AUC.calculation.CSV.file %>% mutate(Patient = as_factor(Patient))
And you'll get a categorical axis.

Related

why is ggplot2 geom_col misreading discrete x axis labels as continuous?

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()

Altair sorting Chart

I'm trying to plot the data of my DataFarme in a groupedChart and I want the columns to preserve the order I gave them before. The data looks as follows (its not all there but its in the same way organized)
dataframe
When I plot it I get the following Graph:
graph
So the months were sorted even though I specified not to sort in the chart. I used the following code:
chart2 = alt.Chart(melted).mark_bar().encode(
column=alt.Column('variable',sort=None),
x=alt.X('room',sort=None),
y=alt.Y('value'),
color='room',
tooltip= ['room', 'value']
)
Does anyone know how I could fix that?
You've already used sort=None, which is the correct way to make scales in a non-faceted chart reflect the input order.
The missing piece is that faceted charts share scales by default (See Scale and Guide Resolution), so each facet is being forced to share an order.
If you make the x scale resolution independent, then each facet should retain the input order:
chart2 = alt.Chart(melted).mark_bar().encode(
column=alt.Column('variable',sort=None),
x=alt.X('room',sort=None),
y=alt.Y('value'),
color='room',
tooltip= ['room', 'value']
).resolve_scale(x='independent')

3d scatter plot with mplot3d with missing frequency as z axis

I am trying to plot some data but I only have my x and y axis given. I would like to get the frequency for my z axis like I would in a histogram. But I don't really want a histogram, rather a scatter plot or a surface plot.
This is my histogram right now, quite messy.
Is it possible to use the function of the histogram to get the frequency of the points but have a scatter/surface plot instead? Or to change the bars of the histogram to something like dots or something similar?
None of my data points have the same value, so I would like them to be sorted in intervals, like [0,0.05] or so, that's why I am trying to find another solution than calculating this.

dual y axis plotting causes data points looks messy at left corner of chart

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

Visualising results from 'effects' package

Any ideas on how to control the tick-label size e.g. on the following code i'd like to control the font size of the words 'Male' and 'Female' on the x axis and the units of 'volunteer' on the Y axis.
Any ideas on how to plot prediction intervals rather than confidence intervals would also be much appreciated.
code from here.
library(effects);
library(stats);
mod.cowles <- glm(volunteer ~ sex + neuroticism*extraversion, data=Cowles, family=binomial);
eff.cowles <- allEffects(mod.cowles, xlevels=list(neuroticism=0:24, extraversion=seq(0, 24, 6)));
plot(eff.cowles, par.strip.text = list(cex = 1.2), xlab=list(cex=2.8), cex=2.5 #par.settings=list(scales=list(cex=1.4),#doesn't work.
#par.scales=list(cex=1.4),#doesn't work.
#scales=list(cex=1.4),#doesn't work
#pscales=list(cex=1.5)#doesn't work.
)
On your first question, I think you're looking for
plot(eff.cowles, cex.lab=.4)
The cex.lab argument is probably what you need.
I don't know an easy way to do prediction intervals rather than confidence intervals, although I'm sure it's possible.