ggplot - geom_raster scale_fill_identity - r

I'm kinda stuck in a matter.
Following the advice of another fellow user in this platform, I was able to plot a raster image and have the pixel assume the values I had assigned I a column, named "colors" in my df.
That was great, but know I need the legends assum the categories I have created in another column. So far, my code and my out put look like this
ggplot(data = Xinjiang_df) + geom_raster(aes(x = x, y = y, fill = colors))+ scale_fill_identity(guide = "legend")
Output
I appreciate any suggestion. Thanks.

Related

Plotting lines between two points in ggplot2

I'm looking for a way to represent a vector coming off of a point given angle and magnitude in ggplot. I've calculated what the endpoint of these vectors should be, but can't figure out a way to plot this properly in ggplot2. In short, given an observation with (X,Y,vec.x,vec.y), how can I plot a line from (X,Y) to (vec.x,vec.y) that does not show (vec.x,vec.y)?
My first instinct was to use geom_line, but this seems to rely on connecting different observations, so I would need to separate each observation into two observations, one with the original point and one with the vector endpoint. However, this seems fairly messy and like there should be a cleaner way to achieve this. Furthermore, this would make it complicated to show the original points but hide the vector points, as they would be plotted within the same geom_point call.
Here's a sample dataset in the form I'm talking about:
test <- tibble(
x = c(1,2,3,4,5),
y = c(5,4,3,2,1),
vec.x = c(1.5,2.5,3.5,4.5,5.5),
vec.y = c(4,3,2,1,0)
)
test %>%
ggplot() +
geom_point(aes(x=x,y=y),color='red') +
geom_point(aes(x=vec.x,y=vec.y),color='blue')
What I'm hoping to achieve is this, but without the blue dots:
Any thoughts? Apologies if this is a duplicated issue. I did some Googling and was unable to find a similar question for ggplot.
test %>%
ggplot() +
geom_point(aes(x=x,y=y),color='red') +
geom_point(aes(x=vec.x,y=vec.y),color='blue') +
geom_segment(
aes(x = x,y = y, xend = vec.x,yend = vec.y),
arrow = arrow(length = unit(0.03,units = "npc")),
size = 1
)
Reference: https://ggplot2.tidyverse.org/reference/geom_segment.html

ggplot2 heatmap plot not equally spread onto background theme

I want to make simple heat map using ggplot, but the heatmap I get is weird. the heatmap plot spreads unequally on the background and I don't know how to fix this.
I have already tried this following code which gave me the heat map plot, but with weird result.
ggplot(data = vmd, aes(x = x, y = y)) +
geom_tile(aes(fill = val)) +
scale_fill_gradientn(colours = mycol)
Someone already said that you can use +coord_equal() to fix the ratio of the X and Y axis.
If you talk about the NA, what I usually do in this case is to clean the data before plotting it.
vmd_clean = vmd[complete.cases(vmd),]
You can also dig into the subset(...) function.
There might be a solution built in ggplot2, but I don't know about it. If someone knows about it, I'd love to learn about.

How to have ggplot grouped bar graph showing percentages according to each group in R?

I have a df with a variable a that contains two strings "stringA1" and "stringA2". It also has a variable b that contains three strings "StringB1", "stringB2" and "stringB3".
What I need is a graph grouped by variable b and showing the percentage of "stringA1" and "stringA2" within each group.
I followed the advice posted on the link below: https://sebastiansauer.github.io/percentage_plot_ggplot2_V2/
And translated to fit my data:
p <- ggplot(STW, aes(x=STW$Q071_3, group=STW$Q01))+
geom_bar(mapping = aes(y = ..prop.., fill = factor(..x..), x=STW$Q071_3),
stat="count")+
scale_y_continuous(labels=scales::percent)+
facet_grid(~Q01)+
p
This, however, gives me a graph with above 100% and I cannot find the error.
I have tried to play around with (..count..)/sum(..count..), but did not get any useful results.
How can I solve this? Thank you and let me know if I should post more information - this is the first post I am making here and I hope to be concise and clear :)
Thank you in advance!
After a lot of trying out, I removed the x=STW$ and the STW$ from group=STW$Q01 and it all works perfectly now, although I do not know the reasons.
Here the fixed code:
p <- ggplot(STW, aes(Q071_3, group=Q01))+
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count")+
scale_y_continuous(labels=scales::percent)+
facet_grid(~Q01)
p

Stacked bar chart in ggplot has separated my variables

I'm trying to make a stacked bar graph but I can't seem to get the protobacteria to group together. This is the code I used
ggplot(data = Bacteria, aes(x = bacteria$Location, y = bacteria$reads, fill = bacteria$Phylum.Division)) +
geom_bar(stat="identity")
Is there something I can add to my code? I've attached a picture of my graph currently.
There are probably duplicate entries of protobacteria in your dataframe, but I cannot reproduce this in a simple example.
I noticed that in your code you use Bacteria and bacteria together. R is case sensitive and it could be you are using 2 dataframes for the plot. Also you can remove the bacteria$ part in the aes statement:
ggplot(data = bacteria, aes(x = Location, y = reads, fill = Phylum.Division)) + geom_bar(stat="identity")
If you want better help, please give a reproducible example of your problem.

barplot 2 bars one stacked the other not

Despite some similar questions and my research I cannot seem to solve my little problem. Please forgive if the answer is very easy and I am being silly....I have a data frame
df<-data.frame(X = c("Germany", "Chile","Netherlands","Papua New Guinea","Cameroon"), R_bar_Ger = c(1300000000, 620000, 550000, 400000, 320000))
I would like to produce a barplot with 2 bars (Country names on x-achsis, amounts on y-achsis).
The left bar should show Germany, the right one should be stacked with the remaining 4 countrys.
Please help and Thank you very much in advance!
One way to solve this is by using ggplot2 and a little bit of manipulating your data frame.
First, add a column to your data frame that indicates which bar a country should be plotted in (Germany or Not-Germany):
df$bar <- ifelse(df$X == "Germany", 1, 0)
Now, create the plot:
ggplot(data = df, aes(x = factor(bar), fill = factor(X), y = R_bar_Ger)) +
geom_bar(stat = "identity") +
scale_y_sqrt() +
labs(x = "Country Group", title = "Square Root Scale", fill = "Country") +
scale_x_discrete(labels = c("Not Germany", "Germany"))
Note that if you're not familiar with ggplot2, only the first two lines are necessary for creating the plot - the others are to make it look nice. Since Germany is orders of magnitude larger than your other countries, this isn't going to look very good without some sort of scaling. ggplot2 has a number of built in scaling commands that might be worth exploring - here, I've added the square root scale so you can that the non-Germany countries actually do get stacked as desired.
The documentation for ggplot2 bar charts can be found here - it's definitely worth a read if you're looking for a powerful plotting tool.
There are a number of ways to skin a cat, and your exact question will often change as you learn new tools. I probably wouldn't have set the problem specification up this way, but sticking as close to your data and barplot as possible, one way to achieve what I think you want is:
with(aggregate(R_bar_Ger ~ X=="Germany", data=df, sum), barplot(R_bar_Ger, names.arg=c("Other", "Germany")))
So what we're doing here is aggregating Germany and non-Germany figures by addition, and then passing those values to the barplot function along with sensible x-axis labels.
You'll need to add an additional column to your data first:
df$group <- ifelse(df$X=="Germany","Germany","Other")
Then we can use the following ggplot approach
library(ggplot)
qplot(x = factor(group), y = R_bar_Ger, data=df, geom = "bar", stat = "identity", fill = factor(X))

Resources