Visualizing just top portion of stacked barplot in ggplot2 in R - r

I made a stacked barplot in ggplot2 in R:
ggplot(Count_dataframe_melt, aes(x = as.factor(variable), y = value, fill = fill)) +
geom_bar(stat = "identity",position="fill")+ scale_y_continuous(name = "Y-axis",labels = scales::percent)
I want to just visualize the top portion of the stacked barplot like so:
I've looked everywhere and can't figure out how to do this. Does anyone know how?

You can use coord_cartesian to "zoom in" on the area you want.
# your plot code...
ggplot(Count_dataframe_melt, aes(x = as.factor(variable), y = value, fill = fill)) +
geom_bar(stat = "identity",position="fill") +
scale_y_continuous(name = "Y-axis",labels = scales::percent) +
# set axis limits in coord_cartesian
coord_cartesian(ylim = c(0.75, 1))
Note that many people consider bar plots that don't start at 0 misleading. A line plot may be a better way to visualize this data.

Since the areas you want to show are less than 20% of the total area, you could flip the bar charts so that you only show the colors areas. Then the y-axis goes from 0-25% and you can use the figure caption to describe that the remaining data is in the gray category.

Related

customising my Facet grid with error bars

Hi everyone I want to do a grid bar plot of my data which is the relative abundance of fungi and make it as neat as possible but when I use facet grid the bar plots don't look right to facilitate comparison. Here is the data:
When I use the code :
theme_set(theme_bw())
facet_plot <- ggplot(my_data, aes(x = depth, y = relative abundance, fill = Treatment)) +
geom_bar(stat = "identity", position = "dodge") +
geom_errorbar(aes(ymin=relative abundance-se, ymax=relative abundance+se), width=.2,position=position_dodge(.9))+
facet_grid(. ~ phylum)
I get a plot that looks like this:
As you can see , the plot looks strange especially the last three barplots. Does anyone know how I can modify my code so each plot has its own y axis or any other way of adjusting the scale?
Best wishes

Stacked Bargraph showing Percentage

Currently i have plotted a stacked bargraph to display the amount of people having delays in each month with the following code
q1%>%
mutate(DepDelay1 =factor(x=DepDelay1, levels=c(0,1), labels=c("No","Yes"))) %>%
filter(Year==2005)%>%
ggplot(aes(x=factor(Month), fill=DepDelay1)) +
geom_bar(position="stack") +
ggtitle("DepDelay of Flight") +
guides(fill=guide_legend("Delay")) +
xlab ("Month")+
ylab ("Flight Count")+
geom_text(aes(label=..count..),
stat='count',
colour = "white",
size = 2.5,
position = position_stack(vjust = 0.5))
Graph Example for position="stack"
However i cannot seem to produce a 100% stacked bargraph after changing from
geom_bar(position="stack")
to
geom_bar(position="fill")
Graph Example for position="fill"
This method works before i adjust the geom_text and adding the filter to my current code.
Anyone know what's the issue ? From what i understand, a Y-variable is needed however i don't have a suitable value to input it from the dataset and i'm trying to plot the count per month.

How to adjust ggplot secondary y-axis to change position of bar plot layer of mixed plot?

I have created a mixed plot (2 lines and 1 bar plot) using GGPlot. The bars represent the percent difference between the two lines on the plot. It looks good except for the positioning of the bars relative to the lines. I would like to be able to adjust the secondary y-axis (i.e. the bar plot axis) to range from -50 to 50 (i.e. centre at 0) without affecting the primary y-axis, which I would like to have start at 0. Does anyone know a way to do this? The current code that I am working on and the resulting plot look like this:
year<-c(1995:2018)
value1<-c(1116.87,3030.85,2676.40,3809.81,2459.74,2666.61,2678.28,3303.45,1839.21,3567.79,4529.22,5838.21,7762.13,8079.70,9615.55,9645.06,8297.23,8974.69,12757.06,13052.86,13670.74,17598.57,17190.01,20192.92)
value2<-c(998.22,3551.52,2421.50,3647.22,2085.44,2558.46,2863.98,3332.18,1606.40,3445.12,4893.11,5486.48,7242.37,7356.78,8810.64,7787.83,7507.25,8442.26,10347.11,11002.82,8783.90,15604.60,14648.09,15368.58)
df1<-data.frame(year,value1,value2)
df1$diff<-((df1$value1-df1$value2)/df1$value1)*100
library(ggplot2)
plot1<-ggplot(df1, aes(x = year)) +
geom_bar(aes(x=year, y = (diff*200), fill="percent diff"), stat="identity") +
geom_line(aes(y=value1,color="A")) +
geom_line(aes(y=value2, color="B")) +
scale_y_continuous(sec.axis = sec_axis(~./200, name = "percent diff")) +
ylab("Value") +
theme(panel.grid.minor = element_blank(),legend.title = element_blank(), legend.spacing.y = unit(-0.1, "cm")) +
scale_color_manual(" ",values=c("A"="#619CFF", "B"="#F8766D", "percent diff"=NA)) +
scale_fill_manual(" ",values=c("percent diff"="gray"))
plot1
To be clear, the below plot (done in Excel) is what I am trying to accomplish.

X axis labels tied to histogram bars instead of following separate rules

When using a histogram with x as a POSIXct value, I'm not sure how you're supposed to line the ticks up with the binsize of the graph.
Setting the tick size to the same as the binsize makes it line a bit off, but the offset adds onto each other until its no longer accurate.
bymonth <- ggplot() +
scale_x_datetime("", breaks = date_breaks("60 days"), labels = date_format("%m-%y")) +
...
lots of geom_rects for background colors
...
theme(legend.title = element_blank()) +
geom_histogram(data=dat, aes(x = iso, fill = name), binwidth = 30*24*60*60, position = 'dodge')
I tried using annotate() as well as experimenting with the spacing of the tick but I think my approach here might be wrong in its own accord
This leads to a graph looking something like this
Which is quite annoying

hHw to make width of bars equal in ggplot2 barplot?

I am trying to make the width of all bars in the following plot equal. Can anybody help me? is it possible? or is there any way to plot this data?
library(ggplot2)
dat <- data.frame(x = c('I','I','I','I','II','II'),
y = LETTERS[1:6],
z = abs(rnorm(6)))
ggplot(dat, aes(y,z))+
geom_bar(stat = "identity") +
facet_wrap(~x,scales="free")
I also tried using arguments size and width inside the geom_bar but its not working.
Really the problem is that each of the facet panels is being forced to be the same size and then the plot inside expands to fill all the available room. With facet_grid you can adjust the space for each facet panel (but you cannot seem to do this with facet_wrap). Try
ggplot(dat, aes(y,z))+
geom_bar(stat = "identity") +
facet_grid(~x,scales="free", space="free_x")
which gives me

Resources