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?
Related
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")))
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:
I am trying to plot a series of variables, which are collected in two-time frames. The structure of data is something like this, the number of observations is 9700, the class is factor.
Please see the structure of the data
I want to plot a barplot like thisI will have a list of the sbs base on each wave.
I have used aggregate function and dplyr, but I could not make a proper structure for the data.
I am very happy that can you help me with it.
Thank you,
As #Tung suggested, you can put your data into long format, and use position_dodge with the plot so bars are next to each other in the plot. Here is an example.
Using tidyr pivot_longer you can put columns that start with "sb" into long form. Then you can filter out rows where the value is zero. unite will combine names - such as sb_1 and x to become sb_1_x.
In this format, it is easier to plot. Use geom_bar to create the bar plot, and use position_dodge2 to put bars next to each other with different wave values. The use of preserve = "single" keeps the bars the same width (in cases where one wave has zero count).
library(tidyverse)
library(ggplot2)
df %>%
pivot_longer(cols = starts_with("sb")) %>%
filter(value != 0) %>%
unite(sb, name, value) %>%
ggplot(aes(x = sb)) +
geom_bar(aes(fill = wave), position = position_dodge2(preserve = "single"))
Plot
I want to create a ggpairs plot colored by a factor, in order to do that I have to keep the factor column in the data frame that goes into ggpairs().
The problem with that is that it adds plots that are done with the factor (the last column and last line in the plot), which I do not want to have in the ggpairs plot (they only add a limited amount of information and make the plot messy).
Is there a way to not show them in the plot, or alternatively color by a factor which is in a separate dataframe?
I was able to remove the whole top part of the plot by using: upper = 'blank' but it doesn't really help as I cannot remove by columns or rows of the ggmatrix.
Is there a way to do this?
I searched for solutions but I didn't find anything relevant
here is an example using the gapminder dataset:
library(dplyr)
library(ggplot2)
library(GGally)
library(gapminder)
gapminder %>%
filter(year == 2002 & continent != 'Oceania') %>%
transmute(lifeExp = lifeExp, log_pop = log(pop), log_gdpPercap = log(gdpPercap), continent = continent) %>%
ggpairs(aes(color = continent, alpha = 0.5))
I get this:
ggpairs with the factor
and I would like to get something like this:
ggpairs colored by factor but without its related plots
You can use the columns argument for this.
From the documentation:
which columns are used to make plots. Defaults to all columns.
In your example output you want only columns 1:3.
... %>%
ggpairs(aes(color = continent, alpha = 0.5), columns = 1:3)
I am facing a difficulty while plotting a parallel coordinates plot using the ggparcoord from the GGally package. As there are two categorical variables, what I want to show in the visualisation is like the image below. I've found that in ggparcoord, groupColumn is only allowed to a single variable to group (colour) by, and surely I can use showPoints to mark the values on the axes, but i also need to vary the shape of these markers according to the categorical variables. Is there other package that can help me to realise my idea?
Any response will be appreciated! Thanks!
It's not that difficult to roll your own parallel coordinates plot in ggplot2, which will give you the flexibility to customize the aesthetics. Below is an illustration using the built-in diamonds data frame.
To get parallel coordinates, you need to add an ID column so you can identify each row of the data frame, which we'll use as a group aesthetic in ggplot. You also need to scale the numeric values so that they'll all be on the same vertical scale when we plot them. Then you need to take all the columns that you want on the x-axis and reshape them to "long" format. We do all that on the fly below with the tidyverse/dplyr pipe operator.
Even after limiting the number of category combinations, the lines are probably too intertwined for this plot to be easily interpretable, so consider this merely a "proof of concept". Hopefully, you can create something more useful with your data. I've used colour (for the lines) and fill (for the points) aesthetics below. You can use shape or linetype instead, depending on your needs.
library(tidyverse)
theme_set(theme_classic())
# Get 20 random rows from the diamonds data frame after limiting
# to two levels each of cut and color
set.seed(2)
ds = diamonds %>%
filter(color %in% c("D","J"), cut %in% c("Good", "Premium")) %>%
sample_n(20)
ggplot(ds %>%
mutate(ID = 1:n()) %>% # Add ID for each row
mutate_if(is.numeric, scale) %>% # Scale numeric columns
gather(key, value, c(1,5:10)), # Reshape to "long" format
aes(key, value, group=ID, colour=color, fill=cut)) +
geom_line() +
geom_point(size=2, shape=21, colour="grey50") +
scale_fill_manual(values=c("black","white"))
I haven't used ggparcoords before, but the only option that seemed straightforward (at least on my first try with the function) was to paste together two columns of data. Below is an example. Even with just four category combinations, the plot is confusing, but maybe it will be interpretable if there are strong patterns in your data:
library(GGally)
ds$group = with(ds, paste(cut, color, sep="-"))
ggparcoord(ds, columns=c(1, 5:10), groupColumn=11) +
theme(panel.grid.major.x=element_line(colour="grey70"))