What is the R equivalent of Python's matplotlib's: plt.tight_layout() function. I keep plotting bar graphs in plotly and the axis labels are constantly cut out. And I want to scale the graphs to match.
N.B I am dynamically producing these graphs with different labels, so can't constantly play with margins to get it to fit.
Below is some fake data that will hopefully illustrate the problem
Organisation<-c("Organisation Alpha","Organisation Beta","Organisation Charlie","Organisation Delta")
Year<-c(2018,2018,2018,2018,2019,2019,2019,2019)
Values<-c(100,50,10,0,20,0,75,70)
Mock_data <- data.frame(Organisation, Year, Values, stringsAsFactors=FALSE)
p<-Mock_data %>% plot_ly(x=~Organisation,y=~Values,type='bar',frame=~Year,colors="Blues")
Think I have managed to resolve the issue:
layout(barmode = 'stack',autosize=F,yaxis=list(title=Title,automargin=TRUE),xaxis=list(automargin=T))
Related
I have a simple issue in interactive heatmap in R. I couldn't figure out how to increase margins (distance between x and y labels with main plot) in heatmaply?
I want to add a little bit of space in ylab (province) from heatmap. Could anybody help me?
enter image description here
You can change the margins with heatmaply by using margins
Since you did not provide any code in your question, I will use an example plot using mtcars and changing the y-axis margins
heatmaply(mtcars, margins = c(0, 200, 0, 0))
If you want to move the y-axis labels away from the graph, then that will be trickier. Currently, there doesn't appear to be a way to do this, however, here is a workaround. There is currently now way to move the y-axis labels in heatmaply. Overall, this is a poor workaround but it appears to be the best solution as of right now.
df <- mtcars
#extract the labels on the y axis. In this example mtcars y-axis labels are the row names
df2 <- as.data.frame(rownames(mtcars))
#you then have to add spaces to the end of the labels and then apply this to the dataframe you are using.
rownames(df) <- paste0(df2$`rownames(mtcars)`, " ")
#you can now run heatmaply on the new labels with added spaces
heatmaply(df)
Overall, plotly has some great features however it lacks in tuneability. heatmaply has even fewer features than plotly so it makes it even harder to tune your graph the way you want to.
This is a ggplot produced by fviz_cluster function. The problem is that I want to single out some data points or even a single one and label it by name. I can easily identify the data points that are on the edges, but what happens for those in the middle of the pack. I thought of approaching this by labelling every data point and then zoom in, but the plot is not interactive and I can't find a way to do that. Any idea on how to approach this would be much appreciated
Here is my code:
r=as.data.frame(colnames(df))
remov=c(1,2,5,7,11,14,16,21,23,24,25,26,89,29:54)
df=df[,-remov]
rem=c(2,3,6,7,8,9,12,13,14,15,16)
rownam=as.data.frame(unique(df$Name))
k=df[,-rem]
k= k[!duplicated(df$Name),]
k <- data.frame(k[,-1], row.names = k[,1])
k=na.omit(k)
kme=kmeans(k,centers = 6,nstart = 25)
str(kme)
fviz_cluster(kme, data = k,geom="point")
If you want an easy workaround to make your graph interactive use the ggplotly() function.
install plotly:
install.packages("plotly")
library(plotly)
plot <- fviz_cluster(kme, data = k,geom="point")
plot %>% ggplotly()
# or
ggplotly(plot)
I am trying to put labels beside some points which are very close to each other on geographic coordinate. Of course, the problem is overlapping labels. I have used the following posts for reference:
geom_text() with overlapping labels
avoid overlapping labels in ggplot2 charts
Relative positioning of geom_text in ggplot2?
The problem is that I do not want to relocate labels but increase the interval of labeling (for example every other 10 points).
I tried to make column as alpha in my dataframe to make unwanted points transparent
[![combined_df_c$alpha=rep(c(1,rep(0,times=11)),
times=length(combined_df_c$time)/
length(rep(c(1,rep(0,times=11)))))][1]][1]
I do not know why it does not affect the plot and all labels are plotted again.
The expected output is fewer labels on my plot.
You can do this by sequencing your dataframe for the labs of geom_text.
I used the build-in dataset mtcars for this, since you did not provide any data. With df[seq(1,nrow(df),6),] i slice the data with 6-steps. This are the labels which get shown in your graph afterwards. You could use this with any steps you want. The sliced dataframe is given to geom_text, so it does not use the original dataset anymore, just the sliced one. This way the amount of points for the labels and the amount of labels are equal.
df <- mtcars
labdf<- df[seq(1,nrow(df),6),]
ggplot()+
geom_point(data=df, aes(x=drat, y=seq(1:length(drat))))+
geom_text(data=labdf,
aes(x=drat, y=seq(1:length(drat))), label=labdf$drat)
The output is as expected: from 32 rows, just 6 get labeled.
You can easily adjust the code for your case.
also: you can put the aes in ggplot() which may be more useful if you use more then just gemo_point. I made it like this, so i can clarify: there is a different dataset used on geom_text()
I am trying to do a scatter plot of 2 time series data - the data is stored in a data frame. The background of the image is quite grainy and axis labels are not visible when I do:
ggplot(data=dat,aes(x,y))+geom_point()
With below, I get only dark vertical lines:
plot(dat$x,dat$y)
plot() and ggplot() did work after applying as.numeric() to the data(as below) but the axis labels are indices[1,2,...] and not the range of actual values.
plot(as.numeric(dat$x),as.numeric(dat$y))
ggplot(data=dat,aes(as.numeric(x),as.numeric(y)))+geom_point()
I cannot post the images here as I am new to this forum.
By default, the data was getting converted into factor while converting from matrix to data.frame. Below code fixed it.
data.frame(mydata,stringsAsFactors = FALSE)
I've been trying to create a 3D bar plot based on categorical data, but have not found a way.
It is simple to explain. Consider the following example data (the real example is more complex, but it reduces to this), showing the relative risk of incurring something broken down by income and age, both categorical data.
I want to display this in a 3D bar plot (similar in idea to http://demos.devexpress.com/aspxperiencedemos/NavBar/Images/Charts/ManhattanBar.jpg). I looked at the scatterplot3d package, but it's only for scatter plots and doesn't handle categorical data well. I was able to make a 3d chart, but it shows dots instead of 3d bars. There is no chart type for what I need. I've also tried the rgl package, but no luck either. I've been googling for more than an hour now and haven't found a solution. I have a copy of the ggplot2 - Elegant Graphics for Data Analysis book as well, but ggplot2 doesn't have this kind of chart.
Is there another freeware app I could use? OpenOffice 3.2 doesn't have this chart either.
Thank you for any hints.
Age,Income,Risk
young,high,1
young,medium,1.2
young,low,1.36
adult,high,1
adult,medium,1.12
adult,low,1.23
old,high,1
old,medium,1.03
old,low,1.11
I'm not sure how to make a 3d chart in R, but there are other, better ways to represent this data than with a 3d bar chart. 3d charts make interpretation difficult, because the heights of the bars and then skewed by the 3d perspective. In that example chart, it's hard to tell if Wisconsin in 2004 is really higher than Wisconsin 2001, or if that's an effect of the perspective. And if it is higher, how much so?
Since both Age and Income have meaningful orders, it wouldn't be awful to make a line graph. ggplot2 code:
ggplot(data, aes(Age, Risk, color = Income))+
geom_line(aes(group = Income))
Or, you could make a heatmap.
ggplot(data, aes(Age, Income, fill = Risk)) +
geom_tile()
Like the others suggested there are better ways to present this, but this should get you started if you want something similar to what you had.
df <- read.csv(textConnection("Age,Income,Risk
young,high,1
young,medium,1.2
young,low,1.36
adult,high,1
adult,medium,1.12
adult,low,1.23
old,high,1
old,medium,1.03
old,low,1.11
"))
df$Age <- ordered(df$Age, levels=c('young', 'adult', 'old'))
df$Income <- ordered(df$Income, levels=c('low', 'medium', 'high'))
library(rgl)
plot3d(Risk ~ Age|Income, type='h', lwd=10, col=rainbow(3))
This will just produce flat rectangles. For an example to create nice looking bars, see demo(hist3d).
You can find a starting point here but you need to add in more lines and some rectangles to get a plot like you posted.