I'm trying to plot the number of observations for each instance of a word, both of which are stored in a data frame.
I can generate the plot with ggplot2, but the y-axis displays "1+e05", "2+e05",...,etc...instead of numerical values.
How can I modify this code so that the y-axis displays numbers instead?
Here is my code:
> w
p.word p.freq
1 the 294571
2 and 158624
3 you 84152
4 for 77117
5 that 71672
6 with 47987
7 this 42768
8 was 41088
9 have 39835
10 are 36458
11 but 33899
12 not 30370
13 all 27079
14 your 26923
15 just 25507
16 from 24497
17 out 22578
18 like 22501
19 what 22150
20 will 21530
21 they 21435
22 about 21184
23 one 20877
24 its 20109
ggplot(w, aes(x = p.word, y = p.freq))+ geom_bar(stat = "identity")
Here is the plot that is generated:
"1e+05" etc are numerical values (scientific notation).
If you want the long notation (e.g. "100,000") use library(scales) and the comma formatter:
library(scales)
ggplot(w, aes(x = p.word, y = p.freq))+ geom_bar(stat = "identity") +
scale_y_continuous(labels=comma)
Related
I am using the R programming language. I have two datasets:
The first dataset:
my_data_1 <- data.frame(read.table(header=TRUE,
row.names = 1,
text="
height weight age
1 13.14600 2882.7709 49
2 12.65080 3183.7991 48
3 13.84154 3138.2280 48
4 15.25780 2786.5297 49
5 15.01213 3006.9687 50
6 14.37567 3286.9644 50
7 12.99385 2881.7667 51
8 15.38893 2916.1883 50
9 14.80093 2791.7292 49
10 15.40423 2427.7706 50
11 17.55129 630.8886 20
12 18.34758 1076.6810 19
13 16.37789 1778.5550 20
14 14.98782 1401.4328 17
15 17.40527 361.3323 20
16 16.53979 869.5829 21
17 16.61986 1712.1686 19
18 17.78508 1961.6090 20
19 16.83144 1043.5052 19
20 18.66166 360.3037 20
"))
The second dataset:
prior_age = rnorm(100000, 50,5)
prior_height = rnorm(100000, 17,1)
prior_weight = rnorm(100000, 3000, 200)
my_data_2 = data.frame(prior_age, prior_height, prior_weight)
(Based on the answer from this post: ggplot combining two plots from different data.frames) I am trying to plot the "densities" of the height variables from both data sets on the same graph. However, both datasets differ in the number of rows.
I tried the following code in R:
library(ggplot2)
ggplot() +
geom_density(data=my_data1, aes(x=height), color='green') +
geom_density(data=my_data2, aes(x=prior_height), color='red')
But this produces the following error:
Error: Aesthetics must be either length 1 or the same as the data (20): x
Can someone please show me how to fix this problem?
Thanks!
Well, from code you provide, I didn't need to change shape of data. Just use guides(... = guide_legend(title = ...)) and scale_colour_discrete to manually change the legend's components.
ggplot() +
geom_density(data=my_data_1, aes(x=height), color='green') +
stat_density(data = my_data_1, aes(x=height, colour="red"), geom="line",position="identity") +
geom_density(data=my_data_2, aes(x=prior_height), color='red') +
stat_density(aes(x=prior_height, colour='green'), geom="line",position="identity") +
guides(colour = guide_legend(title = "new title"),) +
scale_colour_discrete(labels = c( "prior", "measurements"))
I need some help. Here is my data which i want to plot. I want to keep $path.ID on y axis and numerics of all other columns added stepwise. this is a subset of very large dataset so i want to pathID labels attached to each line. and also the values of the other columns with each point if possible.
head(table)
Path.ID sc st rc rt
<chr> <dbl> <dbl> <dbl> <dbl>
1 map00230 1 12 5 52
2 map00940 1 20 10 43
3 map01130 NA 15 8 34
4 map00983 NA 14 5 28
5 map00730 NA 5 3 26
6 map00982 NA 16 2 24
somewhat like this
Thank you
Here is the pseudo code.
library(tidyr)
library(dplyr)
library(ggplot2)
# convert your table into a long format - sorry I am more used to this type of data
table_long <- table %>% gather(x_axis, value, sc:rt)
# Plot with ggplot2
ggplot() +
# draw line
geom_line(data=table_long, aes(x=x_axis, y=value, group=Path.ID, color=Path.ID)) +
# draw label at the last x_axis in this case is **rt**
geom_label(data=table_long %>% filter(x_axis=="rt"),
aes(x=x_axis, y=value, label=Path.ID, fill=Path.ID),
color="#FFFFFF")
Note that with this code if a Path.ID doesn't have the rt value then it will not have any label
p<-ggplot() +
# draw line
geom_line(data=table_long, aes(x=x_axis, y=value, group=Path.ID, color=Path.ID)) +
geom_text(data=table_long %>% filter(x_axis=="rt"),
aes(x=x_axis, y=value, label=Path.ID),
color= "#050505", size = 3, check_overlap = TRUE)
p +labs(title= "title",x = "x-lable", y="y-label")
I had to use geom_text as i had large dataset and it gave me somewhat more clear graph
thank you #sinh it it helped a lot.
I'm trying to plot the following dataframe as bar plot, where the values for the filteredprovince column are listed on a separate column (n)
Usually, the ggplot and all the other plots works on horizontal dataframe, and after several searches I am not able to find a way to plot this "transposed" version of dataframe.
The cluster should group each bar graph, and within each cluster I would plot each filteredprovince based on the value of the n column
Thanks you for the support
d <- read.table(text=
" cluster PROVINCIA n filteredprovince
1 1 08 765 08
2 1 28 665 28
3 1 41 440 41
4 1 11 437 11
5 1 46 276 46
6 1 18 229 18
7 1 35 181 other
8 1 29 170 other
9 1 33 165 other
10 1 38 153 other ", header=TRUE,stringsAsFactors = FALSE)
UPDATE
Thanks to the suggestion in comments I almost achived the format desired :
ggplot(tab_s, aes(x = cluster, y = n, fill = factor(filteredprovince))) + geom_col()
There is any way to put on Y labels not frequencies but the % ?
If I understand correctly, you're trying to use the geom_bar() geom which gives you problems because it wants to make sort of an histogram but you already have done this kind of summary.
(If you had provided code which you have tried so far I would not have to guess)
In that case you can use geom_col() instead.
ggplot(d, aes(x = filteredprovince, y = n, fill = factor(PROVINCIA))) + geom_col()
Alternatively, you can change the default stat of geom_bar() from "count" to "identity"
ggplot(d, aes(x = filteredprovince, y = n, fill = factor(PROVINCIA))) +
geom_bar(stat = "identity")
See this SO question for what a stat is
EDIT: Update in response to OP's update:
To display percentages, you will have to modify the data itself.
Just divide n by the sum of all n and multiply by 100.
d$percentage <- d$n / sum(d$n) * 100
ggplot(d, aes(x = cluster, y = percentage, fill = factor(filteredprovince))) + geom_col()
I'm not sure I perfectly understand, but if the problem is the orientation of your dataframe, you can transpose it with t(data) where data is your dataframe.
I would like to create a multivariate boxplot time series with ggplot2 and I need to have an x axis that positions the boxplots based on their associated dates.
I found two posts about this question: one is Time series plot with groups using ggplot2 but the x axis is not a scale_x_axis so graph is biased in my case. The other one is ggplot2 : multiple factors boxplot with scale_x_date axis in R but the person uses an interaction function which i don't use in my case.
Here is an example file and my code:
dtm <- read.table(text="date ruche mortes trmt
03.10.2013 1 8 P+
04.10.2013 1 7 P+
07.10.2013 1 34 P+
03.10.2013 7 16 P+
04.10.2013 7 68 P+
07.10.2013 7 170 P+
03.10.2013 2 7 P-
04.10.2013 2 7 P-
07.10.2013 2 21 P-
03.10.2013 5 8 P-
04.10.2013 5 27 P-
07.10.2013 5 24 P-
03.10.2013 3 15 T
04.10.2013 3 6 T
07.10.2013 3 13 T
03.10.2013 4 6 T
04.10.2013 4 18 T
07.10.2013 4 19 T ", h=T)
require(ggplot2)
require(visreg)
require(MASS)
require(reshape2)
library(scales)
dtm$asDate = as.Date(dtm[,1], "%d.%m.%Y")
## Plot 1: Nearly what I want but is biased by the x-axis format where date should not be a factor##
p2<-ggplot(data = dtm, aes(x = factor(asDate), y = mortes))
p2 + geom_boxplot(aes(fill = factor(dtm$trmt)))
## Plot 2: Doesn't show me what I need, ggplot apparently needs a factor as x##
p<-ggplot(data = dtm, aes(x = asDate, y = mortes))
p + geom_boxplot(aes( group = asDate, fill=trmt) ) `
Can anyone help me with this issue, please?
Is this what you want?
Code:
p <- ggplot(data = dtm, aes(x = asDate, y = mortes, group=interaction(date, trmt)))
p + geom_boxplot(aes(fill = factor(dtm$trmt)))
The key is to group by interaction(date, trmt) so that you get all of the boxes, and not cast asDate to a factor, so that ggplot treats it as a date. If you want to add anything more to the x axis, be sure to do it with + scale_x_date().
I am quite new in R. I have number of coordinates and I want to plot them in a proper way in R which also presents labels. Moreover, axises should present the lat and long. I have tries ggplot but I cannot fit the data to the code.
id lon lat
1 2 7.173500 45.86880
2 3 7.172540 45.86887
3 4 7.171636 45.86924
4 5 7.180180 45.87158
5 6 7.178070 45.87014
6 7 7.177229 45.86923
7 8 7.175240 45.86808
8 9 7.181409 45.87177
9 10 7.179299 45.87020
10 11 7.178359 45.87070
11 12 7.175189 45.86974
12 13 7.179379 45.87081
13 14 7.175509 45.86932
14 15 7.176839 45.86939
15 17 7.180990 45.87262
16 18 7.180150 45.87248
17 19 7.181220 45.87355
18 20 7.174910 45.86922
19 25 7.154970 45.87058
20 28 7.153399 45.86954
21 29 7.152649 45.86992
22 31 7.154419 45.87004
23 32 7.156099 45.86983
To do this use the geom_text geometry:
ggplot(aes(x = lon, y = lat), data = df) + geom_text(aes(label = id))
This plots the text in the id column on the locations specfied by the columns lon and lat. The data is stored in the data.frame df.
or use:
ggplot(aes(x = lon, y = lat), data = df) + geom_text(aes(label = id)) +
geom_point()
if you want to add both a point and a label. Use the hjust and vjust parameters of geom_text to change the orientation of the label relative to the point. In addition, give each point a color according to the column var by using the color parameter in the geom_point aesthetics:
ggplot(aes(x = lon, y = lat), data = df) + geom_text(aes(label = id)) +
geom_point(aes(color = var))
Do note that ggplot2 cannot deal with the Spatial classes provided by the sp package. Use as.data.frame to convert point (SpatialPoints) and gridsets (SpatialPixels/SpatialGrid) to data.frame's. In addition, use fortify to convert polygon datasets (SpatialPolygons) to data.frame.