r - No line on plotting chart with ggplot using geom_line [duplicate] - r

This question already has answers here:
ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"
(6 answers)
Closed 1 year ago.
I have the below table on data "data_temp" and I'm trying to plot a trend line using "geom_line", but line does not appear.
> head(data_temp,15)
Mês n
1 Janeiro 7432
2 Fevereiro 7077
3 Março 7494
4 Abril 7760
5 Maio 7755
6 Junho 7516
7 Julho 7320
8 Agosto 7084
9 Setembro 7134
10 Outubro 7390
11 Novembro 6973
12 Dezembro 8622
This is the code I used:
ggplot(data = data_temp, aes(x=Mês, y=n))+
geom_line() +
geom_point()
You can see below that the points work, but the line does not appear.
I tried so many codes in a different way, but I haven't suceed so far. I hope anyone can help me.
The x = "Mês", means "Month", but is in portuguese.
Many thanks,
Wil

Try
library(ggplot2)
ggplot(data = data_temp, aes(x=Mês, y=n, group=1))+
geom_line() +
geom_point()
to get
The geom_line-functions needs a group to "know" which dot's should be connected. Since you don't have a grouping variable in your data, you simply add group = 1 to group all dots into the same group.

Related

Setting x-axis in ggplot to end at the last date data is available for [duplicate]

This question already has answers here:
Remove space between plotted data and the axes
(3 answers)
ggplot2: Add secondary x label (year below months)
(2 answers)
Multi-row x-axis labels in ggplot line chart
(3 answers)
Closed 3 years ago.
I have a line graph with the number of visitors staying at a campsite per night. My dataset ends at 10-28-2019, but my graph extends past that date which is unnecessary and provides false information. I want the graph to end at max(nightcounts$date) but am not sure where this should properly go. I tried adding it in line 1 as
aes(max(nightcounts$date), nightcounts$Freq
as an addition to the current aesthetic. Secondly, I am tryin got reformat the x- axis labeling to have the number day below each point, and then the month as a second line below thse points such as
27 28 29 30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
September___________October
1 base <- ggplot(data=nightcounts, aes(nightcounts$date, nightcounts$Freq)) #using ggplot with x = date, y = counts
2 base + geom_line(col="darkorchid4", aes(group = 1)) + #calling geom_line to make a line connecting data points
3 geom_point() + #calling geom_point to add points for each data point
4 labs(title = "Number of Visitors Camping per Night", #adding labels
5 x = "Date",
6 y = "Visitors") +
7 scale_x_date(labels = date_format("%m-%d-%Y"), breaks = date_breaks("week")) +
8 ggsave("Camping by Date.png") #save the image
image of my current plot

ggplot multiple lines in same graph

I am trying to plot multiple gene expressions over time in the same graph to demonstrate a similar profile and then add a line to illustrate the mean of total for each timepoint (like the figure 4b in recent Nature comm article https://www.nature.com/articles/s41467-017-02546-5/figures/4). My data has been normalised to be around 0 so they are all on the same scale.
df2 sample:
variable value gene
1 5 -0.610384193 1
2 5 -6.25967087 2
3 5 -3.773389731 3
50 6 -0.358879035 1
51 6 -6.066341017 2
52 6 -4.202998579 3
99 7 -0.103885903 1
100 7 -6.648844687 2
101 7 -5.041554127 3
I plot the expression levels with ggplot2:
plotC <- ggplot(df2, aes(x=variable, y=value, group=factor(gene), colour=gene)) + geom_line(size=0.5, aes(color=gene), alpha=0.4)
But adding the mean line in red to this plot is proving difficult. I calculated the means and put them in another dataframe:
means
value variable gene
1 -1.5037354 5 50
2 -0.8783492 6 50
3 -0.7769085 7 50
Then tried adding them as another layer:
plotC + geom_line(data=means, aes(x=variable, y=value, color="red", group=factor(gene)), size=0.75)
But I get an error Error: Discrete value supplied to continuous scale
Do you have any suggestions as to how I can plot this mean on the same graph in another color?
Thank you,
Anna
edit: the answer by RG20 is helpful, thanks for pointing out I had the color in the wrong place. However it plots the line outside the rest of the graph... I really don't understand what's wrong with my graph...
enter image description here
plotC + geom_line(data=means, aes(x=variable, y=value, group=factor(gene)), color='red',size=0.75)

How to plot bar plot in R? [duplicate]

This question already has an answer here:
How to create grouped barplot with R [duplicate]
(1 answer)
Closed 3 years ago.
I have a data frame as follows:
reason_code num_stayed num_disconnected
1 60 2
2 113 3
3 212 2
4 451 6
.....
I basically want to plot the bar plot to compare for each reason_code, how many stayed and how many left? And I want to show these side by side.
That is in the same plot. Have two bars for each reason code. One bar in (says) red the other in green.
How do I plot them in R?
You can use the beside argument in barplot to accomplish this. Here's a very quick example:
example <- data.frame(reason_code=c(1,2,3,4),
num_stayed=c(60,113,212,451),
num_dx=c(2,3,2,6))
barplot(height=t(as.matrix(example[c("num_stayed","num_dx")])),beside=TRUE)
Note that I had to transpose it to get the barplot to interpret it correctly. See also this answer from Cross-Validated.
Here's a solution using ggplot:
require(ggplot2)
data = data.frame(reason_code = c(1,2,3,4),
num_stayed = c(60,113,212,451),
num_disconnected = c(2,3,2,6))
data = rbind(data.frame(type = "num_stayed", val = data$num_stayed, reason_code = data$reason_code),
data.frame(type = "num_disconnected", val = data$num_disconnected, reason_code = data$reason_code))
ggplot(data, aes(y=val, x=reason_code, fill=type)) + geom_bar(stat="identity", position="dodge")

How can I use ggplot2 to make a stacked barplot of pretabulated data with each column being a factor on each bar? [duplicate]

This question already has answers here:
Making a stacked bar plot for multiple variables - ggplot2 in R
(3 answers)
Closed 9 years ago.
I have data that has the following format:
revision added removed changed confirmed
1 20 0 0 0
2 18 3 8 10
3 12 8 14 10
4 6 5 11 8
5 0 1 7 11
Each row represents a revision of a document. The first column is the revision number, and the remaining columns represent elements added, removed, changed, and confirmed (ready) in the respective revision. (In reality, there are more rows and columns, this is just an example.) Each number represents the amount of recorded additions, removals, changes, and confirmations in each respective revision.
What I need is a stacked barplot that looks like somthing like this:
I would like to do this in ggplot2. The exact visual look is not important (fonts, colours, and placement of the legend) as long as I can tweak it later. At the moment, it's the general idea I'm looking for.
I've looked at several questions and answers, e.g.
How do I do a Barplot of already tabled data?,
Making a stacked bar plot for multiple variables - ggplot2 in R,
barplot with 3 variables (continous X and Y and third stacked variable), and
Stacked barplot, but they all seem to make assumptions that don't match my data. I've also experimented with something like this:
ggplot(data) + geom_bar(aes(x=revision, y=added), stat="identity", fill="white", colour="black") + geom_bar(aes(x=revision, y=removed), stat="identity", fill="red", colour="black")
But obviously this does not create a stacked barplot because it just drawns the second geom_bar over the first.
How can I make a stacked barplot of my data using ggplot2?
Try:
library(reshape2)
dat <- melt(data, id="revision")
ggplot(dat, aes(x=revision, y=value, fill=variable)) +
geom_bar(stat="identity")

histogram from two column data: number of entries and number in R [duplicate]

This question already has answers here:
Create a histogram for weighted values
(3 answers)
Closed 6 years ago.
This is the head of a data set containing 101302 observations. It is listing vehicle weight, and the number of registrations. I want to plot this as a histogram in R.
r mkg
3 1495
1 1447
1 1401
1 2405
1 2635
2 2515
I need to plot a histogram of the mkg variable, but I need to allow for the number of registrations. I'm not sure how to approach this. I'm sorry, I'm sure this is basic but I've looked all day for an answer and haven't found one that works.
Using ggplot2 package, you can try something like this:
library(ggplot2)
ggplot(df, aes(x = mkg)) + geom_histogram() + facet_wrap(~r)
It will make as many plots as there are unique values in column r.
If you want to plot all histograms on the same plot, you can try this:
library(ggplot2)
ggplot(df, aes(x = mkg, fill = r)) + geom_histogram()

Resources