Thanks in advance for humoring a complete newbie to R. I'm working with some data from the GSS for an online class, and I've created a ggplot facet grid. I'm sure I've done this a super awkward, long way, but I'm trying to get these data points to not overlap each other, but be centered on the columns.
Here's what I've got so far:
I've created a new dataset from the GSS with the variables 'conpress', 'sex', and 'news' -- which refer to the confidence in the press, gender and how often someone reads a newspaper. I wanted to get the percentages, not the counts, which is why I did the ..count..stuff.
gss_press_full <- gss %>% select (conpress, news, sex)
gss_press_clean <-na.omit(gss_press_full)
ggplot(gss_press_clean, aes(x = conpress, y = (..count..)/sum(..count..), fill = sex)) +
geom_bar(aes(y = (..count..)/sum(..count..)), position = position_dodge()) + facet_grid(news~.) +
geom_text(aes(y = ((..count..)/sum(..count..)), label = round((..count..)/sum(..count..), 2)), stat = "count", vjust = -0.25) +
labs(title = "Newspaper readership and Press confidence", y = "Percent", x = "Levels of confidence in the Press")
I have been googling for far too long and can't seem to find a way to adjust these labels atop the columns. It seems to be especially tricky since my y variable is being calculated in my ggplot creation, but again, like a complete novice, that was how I cobbled my way to the output. If someone has help on how to streamline this process, I'd appreciate that too!
( I hope I've included enough code to be helpful!)
Again, thanks for any help!
Related
I’m trying to use facet_wrap in r with 4 plots that have different x-axis. I used scales = “free” which was helpful, but most of the plots touch the top of the graph. I would like there to be space above the tallest bar of the graph, so it doesn’t look like it’s going off above the limit of the graph. I hope that makes sense - this is my first time posting a question here. I have provided the code that I have and a screenshot of one of the graphs. I haven't been able to find any sort of fix without using cow plot which is a bit more advanced than I would like.
Code:
wrap_plot <- df %>% ggplot(aes(x=Race, y = Rate, fill = Sex))+
geom_col(position = "dodge")+
labs(x = "Race/Ethnicity",
y = "Rate per 100,000 population")+
theme_classic()+
facet_wrap(~Disease, scales = "free")+
scale_y_continuous(expand = c(0, 0))
I'm currently working on plotting simple plots using ggplot2.
The graph looks good, but there is one tiny detail I can't fix.
When you look at the legend, it says "Low n" twice. One of them should be "High n".
Here is my code:
half_plot <- ggplot() +
ggtitle(plot_title) +
geom_line(data = plot_dataframe_SD1, mapping = aes(x = XValues, y = YValues_SD1, color = "blue")) +
geom_line(data = plot_dataframe_SD2, mapping = aes(x = XValues, y = YValues_SD2, color = "green")) +
xlim(1, 2) +
ylim(1, 7) +
xlab("Standard Deviation") +
ylab(AV_column_name) +
scale_fill_identity(name = 'the fill', guide = 'legend',labels = c('m1')) +
scale_colour_manual(name = 'Legend',
values =c('blue'='blue','green'='green'),
labels = c(paste("Low ", Mod_column_name), paste("High ", Mod_column_name))
Here is the graph I get in my output:
So do you know how to fix this?
And there is one more thing that makes me curious: I can't remember that I changes anything in this code, but I know that the legend worked just fine a few days ago. I safed pictures I made wih this code and it looks alright..
Also if you have any further suggestions how to upgrade the graph, these suggestions are very welcome too.
When asking questions, it will help us if you provide a reproducible example including the data. With some sample data, there are a couple ways to fix it.
Sample data
library(dplyr)
plot_dataframe_SD1 = data.frame(XValues=seq(1,2,by=.2)) %>%
mutate(YValues_SD1=XValues*2)
plot_dataframe_SD2 = data.frame(XValues=seq(1,2,by=.2)) %>%
mutate(YValues_SD2=XValues*5)
The simplest way to modify your code is to supply the desired color label in the aesthetic.
Mod_column_name = 'n'
half_plot <- ggplot() +
# put the desired label name in the aesthetic
# link describing the bang bang operator (!!) https://www.r-bloggers.com/2019/07/bang-bang-how-to-program-with-dplyr/ geom_line(data=plot_dataframe_SD1,mapping=aes(x=XValues,y=YValues_SD1,color=!!paste('Low',Mod_column_name))) +
geom_line(data=plot_dataframe_SD2,mapping=aes(x=XValues,y=YValues_SD2,color=!!paste('High',Mod_column_name))) +
scale_color_manual(values=c('blue','green'),
labels=c(paste('Low',Mod_column_name),paste('High',Mod_column_name)))
A more general approach is to join the dataframes and pivot the joined df to have a column with the SD values and another to specify how to separate the colors. This makes it easier to plot without having to make multiple calls to geom_line.
# Join the dfs, pivot the SD columns longer, and make a new column with your desired labels
joined_df = plot_dataframe_SD1 %>% full_join(plot_dataframe_SD2,by='XValues') %>%
tidyr::pivot_longer(cols=contains('YValues'),names_to='df_num',values_to='SD') %>%
mutate(label_name=if_else(df_num == 'YValues_SD1',paste('Low',Mod_column_name),paste('High',Mod_column_name)))
# Simplified plot
ggplot(data=joined_df,aes(x=XValues,y=SD,color=label_name)) +
geom_line() +
scale_color_manual(values=c('blue','green'),
labels=c(paste('Low',Mod_column_name),paste('High',Mod_column_name)))
In a ggplot (geom_bar), I'm looking to plot the zero-values in a different color.
Code for the bar-graph itself:
ggplot(Rodeococha, aes(x=Age ,y=Quantity)) +
geom_bar(color="dark red", stat = "identity")
And using the instructions for colouring specific values found on a different page I tried cutting my values into intervals and constructed:
ggplot(data= Rodeococha, aes(x= Age ,y= Quantity)) +
geom_bar(aes(colour = cut(qsec, c(-Inf,0,Inf))), stat = "identity") +
scale_colour_manual(name = "qsec", values = c("(-Inf,0]" = "black",
"(0,Inf]" = "red"))
Atm it gives the error
Error in cut(qsec, c(-Inf, 0, Inf)) : object 'qsec' not found.
Before this error, it also gave a few other errors so instead of taking even more time tackling this one error I thought why not ask advice, maybe there is someone else with a better idea.
Edit: the answer from #Tjebo worked.
For clarification to others: the plot is actually a stacked plot with 7 x-axes each containing multiple bars. This code was just the first x-axis. Showing the zeros in a different color was to make interpretation more easy.
Your code is not reproducible, I am therefore using another data set. First, bar graphs may not be appropriate here. It's difficult to show 'zeros' with bar graphs. I am increasing the line size in order to show the effect, and you will see that this has a quite undesired side effect.
For your question, just use a conditional statement as aesthetic. See below
If this is not what you want, provide better sample data and a desired output.
library(ggplot2)
ggplot(mtcars, aes(x= cyl,y= vs)) +
geom_bar(stat = "identity", size = 3, aes(color = vs == 0)) +
scale_colour_manual(name = "vs", values = c(`TRUE` = 'black',`FALSE` = "red"))
Created on 2020-03-30 by the reprex package (v0.3.0)
I am trying to make a graph, were I have amount up the y-axis (numeric), Office place (categorical) on the x= axis, sorted in regions (categorical)...
What I have tried to do:
My_df %>%
filter(Context == "Humanitarian") %>%
ggplot(aes(Office_abb, Award_USD)) +
geom_histogram(aes(color=Region, fill = Region)) +
theme(legend.position="bottom") +
ggtitle("Overview of office amount pr. region")+
theme(axis.text.x = element_text(angle = 90, hjust = 1))
My error message on the above script is:
Error: stat_bin() must not be used with a y aesthetic.
I wold like it to be in a stacked diagram, or dodged - but when I tried to add
geom_bar(position = "dodge2")
into the equation, it didn't work either
It did however work with geom_point - but that is not the type of visualization that I wish for.
With that i am kind of lost on what to do - I hope that someone can help me move on from here! :-)
I have following code to graph a contracts in different countries.
Country <- CCOM$Principal.Place.of.Performance.Country.Name
Val <- CCOM$Action_Absolute_Value
split <- CCOM$Contract.Category
ggplot(CCOM, aes(x = Country, y = Val, fill = levels(split))) +
geom_bar(stat = "identity")
I want a simple stacked bar chart with the bars colored by the contract category which is the variable "split" (ie. CCOM$Contract.Category).
However when I run the code it produces the graph below:
Why won't gplot separate the spending into three distinct blocks? Why do I get color sections scattered throughout the chart.? I have tried using factor(split) and levels(split) but does not seem to work. Maybe I am putting it in the wrong position.
Ah, I just realized what was going on. You seem scared to modify your data frame, don't be! Creating external vectors for ggplot is asking for trouble. Rather than create Country and Val as loose vectors, add them as columns to your data:
CCOM$Country <- CCOM$Principal.Place.of.Performance.Country.Name
CCOM$Val <- CCOM$Action_Absolute_Value
Then your plot is nice and straightforward, you don't have to worry about order or anything else.
ggplot(CCOM, aes(x = Country, y = Val, fill = Contract.Category)) +
geom_bar(stat = "identity")
as you suggest order provides a solution:
ggplot(CCOM[order(CCOM$split), ], aes(x = Country, y = Val, fill = Contract.Category)) +
geom_bar(stat = "identity")
I have a similar example where I use the equivalent of fill as Contact.Category and it still requires the reordering.