Edit R data frame to plot multiple lines on the same plot - r

I have data in the given format:
(https://i.stack.imgur.com/Y6gFM.png)
so it is categories on the far left (row names), and months are on the top (column names)
And I need to create a plot with a line for each Group.1 category on the same plot, which will have x axis of month and y axis as the corresponding value. Initially I was thinking of creating a separate data frame for each category with month and value as columns, but it seems there should be a better way. Any ideas?
I tried transposing the data frame, which would allow me to get the month and values correctly, but I would not know what category they would be for plotting the lines.

Related

Adding a secondary axis in ggplot corresponding to date when plotting Latitude and Longitude of an individual

I have data containing Latitude and Longitude of an individual. An example of a subset of my data can be found here: https://drive.google.com/file/d/1NWYr8GOwf8WFqfeFf7mUPU2Jt5rkCUYs/view?usp=share_link
The data includes the Latitude and Longitude of locations (approx. every three hours depending on error in fix rates) over a year. There are multiple points per day for the individual. There is also a column corresponding to the elevation of that location.
I am trying to plot the Longitude and Latitude by time (either month or date). So for I have the following code to create this plot:
#read data and format columns;
##month and day as factor for ease of graphing but might need to change to continuous
d<-read.csv("Sample_Data.csv") %>%
mutate(Month=as.factor(Month), ##creates month as factor
Day=as.factor(Day))%>%##create day as factor
arrange(DateTime)##arrange dates
##Plot lat and long- connected by path of travel
trial1<-ggplot(data=d,aes(x=Longitude,y=Latitude))+
geom_path()+
geom_point(size=0.5)+
coord_cartesian()+
theme_bw()
Which results in the following figure:
What I would like to do is add a second axis corresponding to Date or month (pulled from these respective columns in my dataframe), depending on which subsets I will be working with. Essentially I want to create the same figure as above but have the Date (month) as a secondary x-axis so I can visualize paths on a temporal scale as well. I have been using ggplot and am aware I can add a secondary axis through this code, with will duplicate my longitude axis:
Trial1+scale_x_continuous(sec.axis = (~.))
There are lots of resources out there for adding secondary axes, as well as the facet_wrap() feature, however I would like one figure that shows both the temporal and spatial aspects of my data.
Is there a way that I can tell the secondary axis to pull data from the Month Column of my dataset and plot the Lat and Long versus the Month using ggplot?
I am also willing to look into other option in various packages, but want to try and avoid needing to create different data types (move objects for example, or trajectories) as I will need to work further with my data outside of these movement specific packages.
Thanks in advance!

How to extract the unique values of a dataframe in comparison to others from an Upset plot? [R]

I have several data files and I showed their intersects with an upset plot. I now want to know what are the unique values in each dataset? For example, as in this picture, how can I extract the names/values of 232 sets of Thriller category?
I first used union to combine all my data into a single dataframe and then I used setdiff in setdiff(data1,all) to characterise the unique values, but nothing has shown up, while in my real upset plot, I have 10 values unique to my data1.
Thanks.

Make a tile graph in R but with a color key for each column

I have a dataframe composed of 4 categorical variables and 100 observations and would like to plot them as column tiles with colors corresponding to the levels of each variable. How could I do that?
Here is an example data frame:
df3=data.frame(
var1=rep(c(1,2),length(LETTERS)),
var2=rep(c(1,2,3),length(LETTERS)),
var3=rep(c(1,2,3,4,5,6),length(LETTERS)))
In theory, I think I should get values of vars 1-3 assigned each one to a row (letter) and column coordinates. Then, use ggplot2 to plot the tile graph like this:
d = data.frame(row = factor(c(row(df3))),
column = factor(c(col(df3))),
value = c(as.matrix(df3)))
ggplot(d,aes(x=column,y=row,fill=value))+geom_tile()
However,apart of not being sure if i created these coordinates well, I also do not see how to have an independent color key for each original df3's column, in the tile graph.

Create a plot showing the number of items released by year

Trying to create a plot showing the number of items (ex. pop_songs) released by year from a dataframe I have (ex. Music_Charts).
I have a year released column in my dataframe and can use that as the x-variable, but I don't know what I would use for the y-variable to show the boxplot since I have the Top 500 Ranked songs on the dataframe.
Well, based on your very general question, if you have a data frame column with the years for each song, you can easily get the count for that column using table.
table(dataframe$year_released)
That should give you the number of entries for every year, then you can plot them (i'm guessing that's what you need)

R ggplot2 - convert row records to vertical values and use in geom_polygon

I have some items that have different eligibility criteria - specifically in this example two variables each with a min and max the values are allowed to take. I would like to see the coverage of the products by plotting rectangles for each product on a chart that shows the area between the mins and maxs.
How would you go about
converting the records most elegantly to that required by geom_polygon() and
ensuring the shapes produced appear as rectangles
Example
library(data.table)
library(ggplot2)
df<-data.table(Product=letters[1:10], minX=1:10, maxX=5:14, minY= 10:1, maxY=14:5)
df.t<-data.table(rbind( df[,list(Product,X=minX,Y=minY)],
df[,list(Product,X=minX,Y=maxY)],
df[,list(Product,X=maxX,Y=minY)],
df[,list(Product,X=maxX,Y=maxY)]))[
order(Product,X,Y)]
ggplot(df.t,aes(x=X,y=Y,group=Product,fill=Product))+geom_polygon()
NB In this reduced example there are only two criteria, however I have a range of criteria columns and would not want to repeat the exercise above for different combinations.
Use your original data frame df and then geom_rect() as you already have minimal and maximal values for the x and y.
ggplot(df,aes(xmin=minX,xmax=maxX,ymin=minY,ymax=maxY,fill=Product))+geom_rect()

Resources