R ggplot2 - align theme grid with axis tick lines - r

I have a ggplot2 barchart for which I changed the axis ticks. However, the panel grid is adding additional lines that I do not want. How do I remove them?
My problems:
I only want the vertical grid lines that match the x-axis ticks.
The position dodge preserve is not working correctly due to having a group and a fill.
My code:
ggplot(byyear, aes(x = year, y = count, group = venue, colour = venue, fill = type)) +
geom_bar(stat = "identity", position=position_dodge(preserve = "single")) +
# BORDER SO I CAN DISTINGUISH THEM
scale_colour_manual(name = "Venue", values = c("#FFFFFF", "#FFFFFF")) +
# MAKE ALL YEARS APPEAR
scale_y_continuous(labels = number_format(accuracy = 1)) +
scale_x_continuous(breaks = unique(byyear$year)) +
theme(legend.position="bottom",
axis.text.x = element_text(angle = 90, hjust = 1))
The data is of the structure:
year,venue,type,count
2010,venue1,type1,163
2010,venue1,type2,18
2011,venue1,type1,16
...
The plot that I'm obtaining is the following (I removed the legend on the plot)

Related

ggplot - stacked bar-plot: Show mean of bars on top of each stacked bar

This is a screenshot of part of my stacked bar-plot. I would like to have the mean values of each of the bars on top of each of the stacked bars
I have tried to include another df with my means through geom_text in the code.
However, since the dfs differ in size it did not work I think. I also tried to directly include the mean through calculating it with the geom_text function. What are your suggestions?
ggplot(Df_class, aes(x = series, y = Class, group = Month, color = Month + label = Class)) + geom_col(position = "stack",show.legend = T) +
labs(x = "BG ID", y = "Class Total",title ="Class Total") +
theme(axis.text = element_text(colour = "black", size = rel(0.45))) +
geom_text(aes(Df_class_mean$Mean_Class, size = 3, nudge_y = 0, vjust = 0.45))

GGplot is adding geom_line item as a geom_bar item in legend

I'm making sediment profile grain size distribution graphs, with stacked bar charts representing sand, silt and clay and an added line showing the median value for each depth. The graph looks good, yet the legend of my final output is mixing up some of my items.
Here is a breakdown of my code:
GS_as = data.frame(Depth = c(10,30,50,70,90),
clay = c(0.99,0,0,2.86,3.62),
silt = c(55.48,81.48,53.26,79.5,70.71),
sand = c(43.53,18.52,46.74,17.64,25.67))
long = melt(GS_as,id = "Depth")
df = data.frame(Depth = c(10,30,50,70,90),
value = c(34.8,24.84,48.9,12.7,19.73),
variable = c("median","median","median","median","median"))
ggplot(long,aes(x=Depth,y=value,fill=variable)) +
geom_bar(stat="identity") + coord_flip() +
scale_y_continuous(position = "right") +
scale_x_continuous(breaks = seq(10,900,by = 20),trans='reverse') +
scale_fill_grey() +
geom_line(data=df, aes(x= Depth, y = value,group=variable,colour=variable)) +
geom_point(data=df,aes(x= Depth, y = value,group=variable,colour=variable))
The final output is giving me this graph 1
Now, how do I remove median from the legend grayscale of grain sizes, and how do i remove the points from each box in grayscale? The points should only be presented with the median as a separate variable. I've searched long to find a solution, but have not gotten anywhere. I'm guessing I got to my final graph by a strange unintuitive way.
Additionally, if its possible I would also like the median line and points to be black, remove the variables title and group all the items under 1 level.
I appreciate any help you can give.
To fix your first issue with the median showing up in the fill legend you could make fill a locale aes of geom_bar. For a black color you could set the color via scale_color_manual. The legend titles could be set or removed via labs and finally (and as far as I understand you) you could "group all the items under 1 level" via theme options by removing the spacing between the legends and almost all the margin around them.
library(ggplot2)
ggplot(long, aes(x = Depth, y = value)) +
geom_bar(aes(fill = variable), stat = "identity") +
coord_flip() +
scale_y_continuous(position = "right") +
scale_x_continuous(breaks = seq(10, 900, by = 20), trans = "reverse") +
scale_fill_grey() +
geom_line(data = df, aes(x = Depth, y = value, group = variable, colour = variable)) +
geom_point(data = df, aes(x = Depth, y = value, group = variable, colour = variable)) +
scale_color_manual(values = c("black")) +
labs(fill = NULL, color = NULL) +
theme(legend.spacing.y = unit(0, "pt"), legend.margin = margin(1, 0, 0, 0))

R ggplot - Maintain x axis labels after reorder

Hi I am plotting the below using ggplot. I would like the x axis labels to be based on values in column 'Area', instead this code creates x axis labels based on column 'value' - can this be corrected without losing the reordering please?
#plot with reorder
PrevalencePlot <- ggplot(ICSTable4, aes(x = reorder(value, Area),
y = value, fill = Statistical_Significance)) +
geom_col() +
scale_fill_manual(values = colours) +
geom_errorbar(aes(ymin=errorbarlowerplot,
ymax=errorbarhigherplot), width=.2,
# Width of the error bars
position=position_dodge(.9)) +
theme_bw() + geom_text(aes(label = valuelabel), vjust = 2.5,
colour = "black") +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))

Create a dodged barplot with ggplot2

I have the dataset below:
Database<-c("Composite","DB","TC","RH","DGI","DCH","DCH","DCH","LDP")
Unique_Drugs<-c(12672,5130,1425,3090,6100,2019,250,736,1182)
Unique_Targets<-c(3987,2175,842,2308,2413,1441,198,327,702)
db<-data.frame(Database,Unique_Drugs,Unique_Targets)
and I would like to create a dodged bar chart like the picture below:
This plot came from a dataframe like:
The difference is that in the x-axis I want the 7 unique Database names and the fill argument should be the Unique_Drugs and Unique_Targets in order to create 2 colored bars that will display their values. Im not sure how to make it work.
My code is:
p <- ggplot(data = db, aes(Database)) +
geom_bar(position = position_dodge(preserve = "single"), stat="count", aes(fill = colnames(db[2:4])), color = "black")+
coord_flip()+
theme(legend.position="top",
legend.title=element_blank(),
axis.title.x=element_text(size=18, face="bold", color="#000000"), # this changes the x axis title
axis.text.x = element_text(size=14, face="bold", color="#000000"), #This changes the x axis ticks text
axis.title.y=element_text(size=18, face="bold", color="#000000"), # this changes the y axis title
axis.text.y = element_text(size=14, face="bold", color="#000000"))+ #This changes the y axis ticks text
labs(x = "Database") +
labs(y = "Value") +
scale_x_discrete(limits = rev(factor(Database))) +
scale_fill_manual("Databases", values = c("tomato","steelblue3"))
Here's one way to achieve what you want:
library(reshape2)
ggplot(melt(db), aes(x = Database, y = value, fill = variable)) +
geom_col(position = "dodge") + ylab(NULL) + theme_minimal() +
scale_fill_discrete(NULL, labels = c("Drugs", "Targets"))
If you wanted a bar plot only for drugs, there would be no need for melt as you could use y = Unique_Drugs to specify the bar heights (note that since we have heights we use geom_col). In this case, however, we want to specify two kinds of heights. Your words that fill argument should be the Unique_Drugs and Unique_Targets precisely suggest that we need some transformations because ggplot doesn't accept two variables for the same aesthetic. So, using melt we get all the heights as a single variable and get a single variable for fill.

Second Y-Axis in ggplot (R) [duplicate]

This question already has answers here:
ggplot with 2 y axes on each side and different scales
(18 answers)
Closed 4 years ago.
I am trying to make a plot with ggplot in a Shiny app in R and I need to set a second Y-axis in it. This plot has two types of graphics: lines and bars. I would like to represent the bars (depth of precipitation) on the left, and the lines (flows) on the right.
My current code is:
output$plotRout <- renderPlot({
ggplot(totalRR(),aes(x=time)) +
geom_bar(aes(y=mm), stat = "identity",fill = "dodgerblue",color = "black") +
geom_bar(aes(y=NetRain), stat = "identity",fill = "Cyan",color = "black") +
geom_line(aes(y=DirRun, colour = "Direct Runoff"), stat = "identity",color = "Red") +
geom_line(aes(y=BF, colour = "Baseflow"), stat = "identity",color = "Darkorange", linetype = "longdash") +
scale_y_continuous("Rainfall (mm)", sec.axis = sec_axis(~.*10, name = "Flow (m3/s)")) +
xlab("Time (h)")
})
The result is:
This plot has on the left the values of the flows, the values that should be on the right, whereas the values of rainfall (the bars) are not displayed on the plot.
How could I make this plot putting the values of the bars (rainfall) on the left and the second y-axis on the right showing the values of the lines (flows)?
Many thanks in advance.
Victor
One solution would be to make the Flow axis your primary y axis. This involves 1) scaling the data using *10 and then 2) transforming the secondary axis using /10 to get back the correct numbers for the Rainfall axis:
ggplot(totalRR(),aes(x=time)) +
geom_bar(aes(y=10*mm), stat = "identity",fill = "dodgerblue",color = "black") +
geom_bar(aes(y=10*NetRain), stat = "identity",fill = "Cyan",color = "black") +
geom_line(aes(y=10*DirRun, colour = "Direct Runoff"), stat = "identity",color = "Red") +
geom_line(aes(y=10*BF, colour = "Baseflow"), stat = "identity",color = "Darkorange", linetype = "longdash") +
scale_y_continuous("Flow (m3/s)", sec.axis = sec_axis(~./10, name = "Rainfall (mm)")) +
xlab("Time (h)")

Resources