Hi all and apologies if this came up but I could not find 100% match answer to my question...
I have a csv file named zed made of 3 cols and 366 observation:
FTD_DAY, FTD_DATE, FTD imported in R.
FTD_DATE looks like a date (dd/mm/yyy) but R reads it as integer (typeof(zed$FTD_DATE) displays integer in GUID).
I then geom_point the data using:
x <-ggplot(data=zed, aes(x=FTD_DATE,y=FTD)) and the x axis looks ugly since cannot fit 366 dates making it unreadable.
Questions I have:
How can I order dates in x in ascending order?
How can I break coordinates showing only (say) 7 coords maintaining all 366 observations?
How can I align the dates in x axis vertically?
I tried scale_discrete(), scale_log() etc. but went nowhere or got somewhere in the wrong place... can someone please help and let me know what is the solution using ggplot?
Related
Been working on this and haven't been able to find a decent answer.
Basically, I've got a dataset of NBA Player height vs draft year, and I am trying to create a boxplot to show how player height has changed overtime (this is for a hw assignment, so a boxplot is necessary). My dataset (nba_data) looks like the table below, but I have 10k rows ranging from players drafted in the 60s all the way to the 2000s.
player_name
draft_year
height_in
player_a
1998
76
player_b
1972
81
player_c
2012
79
So far the closest I've gotten is
ggplot(data = nba_data, aes(x = draft_year,
y = height_in,
group = cut(x = draft_year, breaks = 5))) +
geom_boxplot()
And this is the result I get. As far as I understand, breaks being set to 5 should separate my years into 5 year buckets, right?
I created the same graph in Excel to get an idea of what it should look like:
I also attempted to create categories with cut, but was unable to apply it to my boxgraph. I mostly code in Python, but have to learn R for a class at school - any help is greatly appreciated.
Thanks!
Edit: Another question I guess would be how the "Undrafted" players would fit into this, since R seems to want to coerce the draft_year column as numerical to fit into a box plot.
From the ?cut help page, the breaks argument is:
breaks
either a numeric vector of two or more unique cut points or a single number (greater than or equal to 2) giving the number of intervals into which x is to be cut.
You gave it a single number, so that's interpreted as the number of intervals.
Instead, you should give it a vector of exact breakpoints, something like breaks = seq(1960, 2020, by = 5).
I'm surprised you think your axis is being numericized--it's definitely a continuous axis, but I've never heard of ggplot doing that to a string or factor input--check your data frame to make sure the "Undrafted" rows are really there, they might have gotten dropped or converted to NA at some point. But that's a good thing for cut, because cut will only work on numerics. I'd suggest cutting the column as numeric to create a bin column, and then replace NAs in the bin column with "Undrafted".
If you don't mind using a package, you can get the effect you want with:
library(santoku)
ggplot(..., aes(..., group = chop_width(draft_year, 5)))
I have the following code to plot a large dataset (450k) in ggplot2
x<-ggplot()+
geom_point(data=data_Male,aes(x=a,y=b),color="Turquoise",position=position_jitter(w=0.2,h=1),alpha=0.1,size=.5,show.legend=TRUE)+
geom_point(data=data_Female,aes(x=a,y=b),color="#FF9999",position=position_jitter(w=0.2,h=1),alpha=0.1,size=.5,show.legend=TRUE)+
theme_bw()
x<-x+geom_smooth(data=data_Male,aes(x=a,y=b,alpha="Male"),method="lm",colour="Blue",linetype=1,se=T)+
geom_smooth(data=data_Female,aes(x=a,y=b,alpha="Female"),method="lm",colour="Dark Red",linetype=5,se=T)+
geom_smooth(data=data_All,aes(x=a,y=b,alpha="All"),method="lm",colour="Black",linetype=3,se=T)+
scale_fill_discrete(name="Key",labels=c("Female","Male","All"))+
scale_colour_discrete(name="Plot Colour",labels=c("Female","Male","All"))+
scale_alpha_manual(name="Key",
values=c(1,1,1),
breaks=c("Female","Male","All"),
guide=guide_legend(override.aes=list(linetype=c(5,1,3),name="Key",
shape=c(16,16,NA),
color=c("Dark Red","Blue","Black"),
fill=c("#FF9999","Turquoise",NA))))
How can I change the order in which points are plotted? I have seen answered questions here dealing with a single dataframe but I am working with several dataframes so I cannot re-order the rows or ask ggplot to plot by certain criteria from within the dataframe. You can see an example of the kind of problem that this causes in the attached picture: the Female points are plotted on top of the Male points. Ideally I would like to be able to plot all the points in a random order, so that one "cloud" of points is not plotted on top of the other, obscuring it (N.B. the image shown doesn't include the "All" line).
Any help would be appreciated. Thank you.
I belive this is not possible. The following should work though:
You'd have to paste the two data frames together to df. The new data frame will appear sorted by male and female.
You can then suffle the new data frame:
set.seed(42)
rows <- sample(nrow(df))
male_female_mixed <- df[rows, ]
Then you can plot male_female_mixed
I want a line graph of around 145 data observations using R, the format of data is as below
Date Total Confirmed Total Deceased
3-Mar 6 0
4-Mar 28 0
5-Mar 30 5
.
.
.
141 more obs like this
I'm new to ggplot 2 in R so i don't know how to get the graph, I tried plotting the graph, but the dates
in x-axis becomes overlaped and were not visible. I want line graph of Total confirmed column and the Total Deceased column together with dates in the x- axis, please help and please also tell me how to colour the line graph, i want a colorfull graph, so... Please Do help in your busy schedule.. thank you so much...
Similar questions like this gives a lot of error, so I would like an answer for my specific requirements.
There are a lot of resources to help you create what you are looking to do - and even quite a few questions already answered here. However, I understand it's tough starting out, so here's a quick example to get you started.
Sample Data:
df <- data.frame(
dates=c('2020-01-01','2020-02-01','2020-03-03','2020-03-14','2020-04-01'),
var1=c(13,15,18,29,40),
var2=c(5,8,11,13,18)
)
If you are plotting by date on your x axis, you need to ensure that df$dates is formatted as a "Date" class (or one of the other date-like classes). You can do that via:
df$dates <- as.Date(df$dates, format='%Y-%m-%d')
The format= argument of as.Date() should follow the conventions indicated in strptime(). Just type ?striptime in your console and you can see in the help for that function how the various terms are defined for format=.
The next step is very important, which is to recognize that the data is in "wide" format, not "long" format. You will always want your data in what is known as Tidy Data format - convenient for any analysis, but necessary for ggplot2 and the related packages. In your data, the measure itself is numbers of cases and deaths. The measure itself is number of people. The type of the measure is either cases or deaths. So "number of people" is spread over two columns and the information on "type of measure" is stuck as a name for each column when it should be a variable in the dataset. Your goal should be to gather() those two columns together and create two new columns: (1) one to indicate if the number is "cases" or "deaths", and (2) the number itself. In the example I've shown you can do this via:
library(dplyr)
library(tidyr)
library(ggplot2)
df <- df %>% gather(key='var_name', value='number', -dates)
The result is that the data frame has columns for:
dates: unchanged
var_name: contains either var1 or var2 as a character class
number: the actual number
Finally, for the plot, the code is quite simple. You apply dates to the x aesthetic, number to y, and use var_name to differentiate color for the line geom:
ggplot(df, aes(x=dates, y=number)) +
geom_line(aes(color=var_name))
I have a data frame with two factors: Peel - either "Standard" or "Delay" and Wafer - a number of a wafer but which I want as a factor:
**Peel** **Wafer**
Standard 122
Standard 123
Delay 124
Delay 125
(sorry I am trying to post real data but it seems to come out in a dodgy format)
When I boxplot my data for a variable against both factors, I get gaps on the x axis where there is no data:
boxplot(Von.fwd~Wafer*Peel, data=df, las=2)
I have tried posting an image but apparently I need 10 reputation to do this.
The data is missing because it doesn't exist. I just dont want it to plot the gap. I have looked at the droplevel code but I dont want to drop either of my factors, just certain combinations of the factors.
Is there a way to tell R not to plot crossed factors where there is no data?
Many thanks
Pete
One option is to combine Peel and Wafer into a new factor like so (assuming your data.frame is called df):
Edit
Sorry did not think that through:
df$NewFactor<-paste(df$Peel,df$Wafer)
df$NewFactor<-factor(df$NewFactor)
That will give you each combination as a factor, but no missing combinations. Then you can use df$newFactor in your boxplot function.
I have a problem where I need to import a two column (first column being the x-axis and the second being the y-axis) .txt-file into R and I need to do this so that R reads it from bottom to top.
Here is what I did so far:
data<-read.table("data.txt",skip=1910,nrow=132982)
plot(data,type="l")
After this, I have the desired plot, but I wish this to be reversed horizontally. What would be the most convenient way to do this?
I tried
datar<-rev(data)
after import but it reversed the columns by switching the x-values to y-axis and y-values to x-axis. I wish to reverse the columns so that the last value in both columns will be the first in their columns without the columns switching places with each other.
I think that most convenient would be to revert the file during import as the file has over 130 000 rows and it is very cumbersome to work with.
Thank you in advance!
I may be daft - but I don't see how order of reading x-y data would affect the position x and y coordinates.
maybe you want something like this:
data <- read.csv(text= "
x,y
1,1
2,2
3,4")
plot(data,xlim=c(3,1))
(x axis goes down from 3 left to 1 right)