I want to create a line chart with two lines in one plot using ggplot. However, one line chart has missing values in-between:
year<-c(1990,1991,1992,1993)
v1<-c(1,NA,NA,2)
v2<-c(2,3,1,2)
I want the second line (v2) to connect its first value in 1990 with its last one in 1993. Is this possible using ggplot?
Try this approach reshaping your data:
library(ggplot2)
library(dplyr)
library(tidyr)
#Data
year<-c(1990,1991,1992,1993)
v1<-c(1,NA,NA,2)
v2<-c(2,3,1,2)
df <- data.frame(year,v1,v2)
#Plot
df %>% pivot_longer(-year) %>%
filter(!is.na(value)) %>%
ggplot(aes(x=factor(year),y=value,color=name,group=name))+
geom_point()+
geom_line()+xlab('year')+
labs(color='Var')
Output:
Related
I have used the following code
ggplot(IncomeGroup_count,
aes(x=Income_Group,
y= Percentage,
colour = 'lightblue'))
But the only thing it produces is the x and y axis with no bars.
It looks like you are missing the geom_bar function. Typically ggplot works as the base function for fitting whatever data you are using and then the geom functions "draw" the plot using that data. Here I have constructed a bar plot using data in R since you have not supplied your own data in your question.
#### Load Library ####
library(tidyverse)
#### Plot ####
airquality %>%
ggplot(aes(x=is.na(Ozone)))+
geom_bar()
Which gives you this bare bones bar plot. It takes the NA values from the airquality dataset, then draws bars thereafter plotting the NA values by "TRUE" for NA and "FALSE" for not NA:
Edit
Again it's difficult to guess what is going wrong if you don't share your data, so I'm assuming based off your comments below that you are trying to plot y explicitly as a count. As suggested in the comments, geom_col may be better for this. Using a different example from the same dataset:
airquality %>%
group_by(Month) %>%
summarise(Mean_Ozone = mean(Ozone, na.rm=T)) %>%
ggplot(aes(x=Month,
y=Mean_Ozone))+
geom_col()
You get this:
I'm studying R and made a simple data to draw a scatter plot.
Here is the simple code that I made:
library(lubridate)
df <- data.frame(date = c(20120324,20120329,20121216,20130216,20130725,20130729,20130930,20131015,20131124,20131225),
math=c(54,32,45,78,89,12,33,46,79,100),
physics=c(100,100,23,24,55,66,34,37,89,39))
df$date<-ymd(df$date)
math<-df$math
physics<-df$physics
date<-df$date
plot(date,math,xlab="year",ylab="math")
par(new=TRUE)
plot(date,physics,xlab="year",ylab="physics")
What I was trying to do was to draw the scatter plots simultaneously in one plane and give them labels to differentiate each other. So my goal is to plot math and physics using different colors. I think red and blue will be fine. I want to make x axis to represent the year. In date column, 20120324 means March 24th, 2012.
You can use functions in tidyverse package to pivot your data into the longer format, and then plot using ggplot.
library(tidyverse)
df %>%
pivot_longer(-date) %>%
ggplot(aes(date, value, colour=name)) +
geom_point() +
scale_x_date(breaks="year",
date_labels="%Y",
limits=as.Date(c("2012-01-01","2014-01-01")))
when using the simple R boxplot function, I can easily place my dataframe directly into the parenthesis and a perfect boxplot emerges, eg:
baseline <- c(0,0,0,0,1)
post_cap <- c(1,5,5,6,11)
qx314 <- c(0,0,0,3,7)
naive_capqx <- data.frame(baseline, post_cap, qx314)
boxplot(naive_capqx)
this is an image of the boxplot made with the simple R boxplot function
However, I need to make this boxplot slightly more aesthetic and so I need to use ggplot. When I place the dataframe itself in, the boxplot cannot form as I need to specify x, y and fill coordinates, which I don't have. My y coordinates are the values for each vector in the dataframe and my x coordinates are just the name of the vector. How can I do this using ggplot? Is there a way to reform my dataframe so I can split it into coordinates, or is there a way ggplot can read my data?
geom_boxplot expects tidy data. Your data isn't tidy because the column names contain information. So the first thing to do is to tidy your data by using pivot_longer...
library(tidyverse)
naive_capqx %>%
pivot_longer(everything(), values_to="Value", names_to="Variable") %>%
ggplot() +
geom_boxplot(aes(x=Variable, y=Value))
giving
Turn the df into a long format df. Below, I use gather() to lengthen the df; I use group_by() to ensure boxplot calculation by key (formerly column name).
pacman::p_load(ggplot2, tidyverse)
baseline <- c(0,0,0,0,1)
post_cap <- c(1,5,5,6,11)
qx314 <- c(0,0,0,3,7)
naive_capqx <- data.frame(baseline, post_cap, qx314) %>%
gather("key", "value")) %>%
group_by(key)
ggplot(naive_capqx, mapping = aes(x = key, y = value)) +
geom_boxplot()
I am trying to make a grouped barplot and I am running into trouble. For example, if I was using the mtcars dataset and I wanted to group everything by the 'vs' column (col #8), find the average of all remaining columns, and then plot them by group.
Below is a very poor example of what I am trying to do and I know it is incorrect.
Ideally, mpg for vs=1 & vs=0 would be side by side, followed by cyl's means side by side, etc. I don't care if aggregate is skipped for dyplr or if ggplot is used or even if the aggregate step is not needed...just looking for a way to do this since it is driving me crazy.
df = mtcars
agg = aggregate(df[,-8], by=list(df$vs), FUN=mean)
agg
barplot(t(agg), beside=TRUE, col=df$vs))
Try
library(ggplot2)
library(dplyr)
library(tidyr)
df %>%
group_by(vs=factor(vs)) %>%
summarise_each(funs(mean)) %>%
gather(Var, Val, -vs) %>%
ggplot(., aes(x=Var, y=Val, fill=vs))+
geom_bar(stat='identity', position='dodge')
Or using base R
m1 <- as.matrix(agg[-1])
row.names(m1) <- agg[,1]
barplot(m1, beside=TRUE, col=c('red', 'blue'), legend=row.names(m1))
I'm using the ggradar package which makes very nice radar plots. I can use it to create one radar plot with multiple lines using a column called "group". Or, I can create multiple radar facets with a single line. But, I can't have facets with multiple lines (groups). Is this possible?
Note I'm using ggradar (https://github.com/ricardo-bion/ggradar) and not ggiraphExtra::ggRadar
library(ggradar)
library(scales)
set.seed(123)
gdat <- tibble(
facet=c('A','A','B','B'),
group=c(2010,2020,2010,2020),
var1=rescale(runif(4)),
var2=rescale(runif(4)),
var3=rescale(runif(4)))
What I would like to have is two panels (A and B), each with two lines (2010, 2020). The following does not work.
gdat %>%
ggradar() +
facet_wrap(vars(facet))
NULL
However, if I only select one "facet" I can use group to plot 2010, 2020 on the same plot.
gdat %>%
filter(facet=='A') %>%
select(-facet) %>%
ggradar()
Or, I can have multiple facets, but each only has one line
gdat %>%
filter(group==2010) %>%
mutate(group=facet) %>% #need to rename "facet" to "group"
select(-facet) %>%
ggradar() +
facet_wrap(vars(group))
Is it possible to have two facets two lines?